在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打印的输出
posted @ 2017-11-27 21:51  非法关键字  阅读(1263)  评论(0编辑  收藏  举报