zoukankan      html  css  js  c++  java
  • 在Unity3d中使用Google.ProtocolBuffers

    1. 通过Nuget下载Google.ProtocolBuffers,在目录中找到net35下的文件,放入unity3d中作为插件
    2.  1 PersonMessage.Builder personBuilder = PersonMessage.CreateBuilder();
       2         personBuilder.Id = 123;
       3         personBuilder.Age = 33;
       4         personBuilder.Sex = PersonMessage.Types.Sex.Male;
       5         personBuilder.Name = "protobuf";
       6         personBuilder.Phone = "1333333333";
       7         var person = personBuilder.Build();
       8         // 序列化到文件
       9         using (var stream = File.Create("person.bin"))
      10         {
      11             CodedOutputStream cos = CodedOutputStream.CreateInstance(stream);
      12             person.WriteTo(cos);
      13             cos.Flush();
      14         }
      15 
      16         // 从文件反序列化到对象
      17         using (var stream = File.OpenRead("person.bin"))
      18         {
      19             CodedInputStream cis = CodedInputStream.CreateInstance(stream);
      20             PersonMessage pm = PersonMessage.ParseFrom(cis);
      21             Debug.Log("从person.bin反序列化到对象:");
      22             Debug.LogFormat("person's name:{0}, person's age:{1}, person's phone:{2}", pm.Name, pm.Age, pm.Phone);
      23         }

      查看生成的person.bin

    3. 执行后unity3d打印的输出
  • 相关阅读:
    [BZOJ3202][SDOI2013]项链
    Educational Codeforces Round 50
    [agc23E]Inversions
    [CF1016G]Appropriate Team
    [CF765F]Souvenirs
    [Luogu3733][HAOI2017]八纵八横
    [Luogu4609][FJOI2016]建筑师
    [BZOJ2159]Crash 的文明世界
    【学习笔记】Nim积
    PKUWC2020游记
  • 原文地址:https://www.cnblogs.com/linxmouse/p/7906362.html
Copyright © 2011-2022 走看看