Protobuf

17 篇文章 0 订阅
3 篇文章 0 订阅
  1. 下载protobuf-net_r668.zip(提取码kfwl),解压,把Full/unity内的所有文件复制到unity的Plugins目录内
  2. 创建协议文件,此处命名为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表示可以拥有多个。

  1. 将chatapp.proto复制到protobuf文件夹中ProtoGen文件夹内
  2. 启动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
  1. 在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();
	}
  1. 从byte数组中解析User
	// bs 是一个byte数组,它应当是从网络或文件中得到
	using(System.IO.MemoryStream stream = new System.IO.MemoryStream stream(bs))
	{
		ChatApp.User user = ProtoBuf.Serializer.Deserialize<ChatApp.User>(stream);
	}
  1. 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提供了一个方法解决这个问题。

  1. 在vs中创建一个c#的Class Library工程,将前面生成的chatapp.cs添加到这个工程
  2. 添加protobuf-net.dll(位于Full/unity文件夹)的引用
  3. 编译当前工程,生成dll文件,假设名字叫ProtoLib.dll
  4. 添加一个新的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是生成的文件名。

  1. 编译当前工程,在输出目录找到.exe文件运行,生成ChatSerializer.dll。将ChatSerializer.dll和ProtoLib.dll复制到unity Plugins中。在unity工程中已经不需要chatapp.cs这个文件,dll文件中已经包括了它。
  2. 在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();
	}
  1. 反序列化
	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));
	}
  1. 泛型封装
	// 序列化
	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版 金玺曾

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值