- 通过Nuget下载Google.ProtocolBuffers,在目录中找到net35下的文件,放入unity3d中作为插件
PersonMessage.Builder personBuilder = PersonMessage.CreateBuilder();
personBuilder.Id = ;
personBuilder.Age = ;
personBuilder.Sex = PersonMessage.Types.Sex.Male;
personBuilder.Name = "protobuf";
personBuilder.Phone = "";
var person = personBuilder.Build();
// 序列化到文件
using (var stream = File.Create("person.bin"))
{
CodedOutputStream cos = CodedOutputStream.CreateInstance(stream);
person.WriteTo(cos);
cos.Flush();
} // 从文件反序列化到对象
using (var stream = File.OpenRead("person.bin"))
{
CodedInputStream cis = CodedInputStream.CreateInstance(stream);
PersonMessage pm = PersonMessage.ParseFrom(cis);
Debug.Log("从person.bin反序列化到对象:");
Debug.LogFormat("person's name:{0}, person's age:{1}, person's phone:{2}", pm.Name, pm.Age, pm.Phone);
}查看生成的person.bin
- 执行后unity3d打印的输出