ProtoBuf 3
ProtoBuf
如今Google官方的ProtoBuf已经出了ProtoBuf的C-sharp版本,使用proto3语言标准,使用库文件需要.Net4.x,需要在Unity的PlayerSettings中将.Net旧版本切换至4.xProtobuf与BinaryFormatter比较
可以参阅这篇目的
提升序列化反序列化速度,减小二进制文件大小提高传输效率,协议文件更具有可读性。定义协议格式
首先是包体说明,防止在不同项目中命名冲突syntax = “proto3”;
package tutorial;如果没有用以下官方例子中的方式将csharp_namespace指定一个命名空间的话,将会默认使用package来进行匹配。
option csharp_namespace = “Google.Protobuf.Examples.AddressBook”;
接着是定义message,可以使用一些基本类型作为字段类型,甚至还能在其它的message中使用已经定义的message类型作为字段类型。
message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;

本文介绍了Google ProtoBuf 3的C#版本,包括其与BinaryFormatter的比较,ProtoBuf的优势,如何定义协议格式,以及如何在C#中编译和使用protobuf文件。通过实例展示了序列化和反序列化过程,同时提供了开发流程的指导。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



