- 下载protobuf-net_r668.zip(提取码kfwl),解压,把Full/unity内的所有文件复制到unity的Plugins目录内
- 创建协议文件,此处命名为chatapp.proto
package ChatApp;
message User{
required string name=1;
required string chat=2;
optional string email=3;
}
message Chat{
repeated User user=1;
}
将package看作是名称域,message看作是class。
required表示这是个必须的数据,optional表示可选的。
repeated表示可以拥有多个。
- 将chatapp.proto复制到protobuf文件夹中ProtoGen文件夹内
- 启动cmd,将路径设置到ProtoGen,执行
protogen -i:chatapp.proto -o:chatapp.cs
生成chatapp.cs,复制到客户端和服务器端。
因为每次修改.proto文件都要重新生成新的.cs文件,为了方便,可以将生成.cs文件的脚本写在一个.cmd文件中,这样每次操作只需要选择这个文件即可。
.cmd放在protobuf文件夹内
ProtoGen\protogen -i:chatapp.proto -o:chatapp.cs
或
@echo off
set fileName=%~nx1
set filePath=%~dp1%~nx1
echo file path: %filePath%
set newFileName=%fileName:~0,-6%.cs
echo %newFileName%
ProtoGen\protogen -i:%fileName% -o:%newFileName%
pause
- 在c#中序列化User
ChatApp.User user = new ChatApp.User();
user.name = "aaa";
user.chat = "bbb";
using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
ProtoBuf.Serializer.Serialize<ChatApp.User>(stream, user);
Byte[] bs = stream.ToArray();
}
- 从byte数组中解析User
// bs 是一个byte数组,它应当是从网络或文件中得到
using(System.IO.MemoryStream stream = new System.IO.MemoryStream stream(bs))
{
ChatApp.User user = ProtoBuf.Serializer.Deserialize<ChatApp.User>(stream);
}
- repeat类型的数据在c#实际是一个数组
ChatApp.Chat chat;
chat.user.Add(user);
E/Unity: PlatformNotSupportedException: Operation is not supported on this platform.
at ProtoBuf.Compiler.CompilerContext..ctor (System.Type associatedType, System.Boolean isWriter, System.Boolean isStatic, ProtoBuf.Meta.TypeModel model, System.Type inputType) [0x000d1] in <46c30dcdddb14f47b90a4218bec05eb9>:0
at ProtoBuf.Compiler.CompilerContext.BuildSerializer (ProtoBuf.Serializers.IProtoSerializer head, ProtoBuf.Meta.TypeModel model) [0x00007] in <46c30dcdddb14f47b90a4218bec05eb9>:0
Rethrow as InvalidOperationException: It was not possible to prepare a serializer for: ChatApp2.User
at ProtoBuf.Compiler.CompilerContext.BuildSerializer (ProtoBuf.Serializers.IProtoSerializer head, ProtoBuf.Meta.TypeModel model) [0x00088] in <46c30dcdddb14f47b90a4218bec05eb9>:0
at ProtoBuf.Serializers.CompiledSerializer..ctor (ProtoBuf.Serializers.IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x0000d] in <46c30dcdddb14f47b90a4218bec05eb9>:0
at ProtoBuf.Serializers.CompiledSerializer.Wrap (ProtoBuf.Serializers
修复:把 api compotibility level 改为 .Net 4.x即可(此处使用的2018.4.30,默认是.Net Standard 2.0)
ProtoBuf在IOS平台的处理
.Net版本的ProtoBuf用到了c#的反射功能,这个功能在IOS系统中是不能使用的,好在.Net版本的ProtoBuf提供了一个方法解决这个问题。
- 在vs中创建一个c#的Class Library工程,将前面生成的chatapp.cs添加到这个工程
- 添加protobuf-net.dll(位于Full/unity文件夹)的引用
- 编译当前工程,生成dll文件,假设名字叫ProtoLib.dll
- 添加一个新的c#控制台工程,将protobuf-net.dll和ProtoLib.dll添加到工程中,添加代码
class Program
{
static void Main(string[] args)
{
ProtoBuf.Meta.RuntimeTypeModel model = ProtoBuf.Meta.RuntimeTypeModel.Create();
model.Add(typeof(ChatApp.Chat), true);
model.Add(typeof(ChatApp.User), true);
model.Compile("ChatSerializer", "ChatSerializer.dll");
}
}
首先创建了一个RuntimeTypeModel对象,然后将.proto文件中定义的类都添加到该对象中,最后的参数ChatSerializer是类的名称域,ChatSerializer.dll是生成的文件名。
- 编译当前工程,在输出目录找到.exe文件运行,生成ChatSerializer.dll。将ChatSerializer.dll和ProtoLib.dll复制到unity Plugins中。在unity工程中已经不需要chatapp.cs这个文件,dll文件中已经包括了它。
- 在c#中序列化User,与之前代码不同
ChatApp.User user = new ChatApp.User();
user.name = "aaa";
user.chat = "bbb";
byte[] bs;
using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
ProtoBuf.Meta.RuntimeTypeModel model = ChatSerializer.Create();
model.Serialize(stream, user);
bs = stream.ToArray();
}
- 反序列化
ChatApp.User user2;
using(System.IO.MemoryStream stream = new System.IO.MemoryStream(bs))
{
ProtoBuf.Meta.RuntimeTypeModel model = ChatSerializer.Create();
user2 = (ChatApp.User)model.Deserialize(stream, null, typeof(ChatApp.User));
}
- 泛型封装
// 序列化
public static byte[] ProtoRuntimeSerialize<T>(T t)
{
byte[] bs;
using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
ProtoBuf.Meta.RuntimeTypeModel model = ChatSerializer.Create();
model.Serialize(stream, t);
bs = stream.ToArray();
}
return bs;
}
// 反序列化
public static T ProtoRuntimeDeserialize<T>(byte[] bs)
{
T t = default(T);
using(System.IO.MemoryStream stream = new System.IO.MemoryStream(bs))
{
ProtoBuf.Meta.RuntimeTypeModel model = ChatSerializer.Create();
t = (T)model.Deserialize(stream, null, typeof(T);
}
return t;
}
使用
ChatApp.User user = new ChatApp.User();
user.name = "aaa";
user.chat = "bbb";
byte[] bs = ProtoRuntimeSerialize<ChatApp.User>(user);
ChatApp.User user2;
user2 = ProtoRuntimeDeserialize(ChatApp.User)(bs);
protobuf除了可以应用到网络中,还可以应用到文件存储中。
Unity3D\2D手机游戏开发第2版 金玺曾