Unity当中使用Protobuf-net进行序列化和反序列化
2019-02-24 10:33
441 查看
probuffer-net可以在https://code.google.com/p/protobuf-net/downloads/list下载(需翻墙)
或者我创建了一个百度云网盘大家可以下载链接:https://pan.baidu.com/s/10CAXDvarJIAxj1faquh0CQ 提取码:e43l
首先我们要创建一个Test.proto文件来进行测试
message ReqLogin { optional string account = 1; // 账号 optional string password = 2; // 密码 }
我们这里要用到一个工具ProtoGen,这个工具在我上传的百度云网盘里或者我给出的google的那个网站中也有下载
为了快捷编译我创建了一个批处理文件run.bat
@echo off for /f "delims=" %%i in ('dir /b proto "proto/*.proto"') do protogen -i:proto/%%i -o:cs/%%~ni.cs pause
这个文件的意思是会编译proto文件夹下的.proto文件为.cs文件并放在cs文件夹下
如图所示路径即可正常编译
将.proto文件放入proto文件夹下双击运行run.bat。会在cs文件夹下生成test.cs文件。打开来是这样的
namespace proto.Test { [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"ReqLogin")] public partial class ReqLogin : global::ProtoBuf.IExtensible { public ReqLogin() {} private string _account = ""; [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"account", DataFormat = global::ProtoBuf.DataFormat.Default)] [global::System.ComponentModel.DefaultValue("")] public string account { get { return _account; } set { _account = value; } } private string _password = ""; [global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"password", DataFormat = global::ProtoBuf.DataFormat.Default)] [global::System.ComponentModel.DefaultValue("")] public string password { get { return _password; } set { _password = value; } } private global::ProtoBuf.IExtension extensionObject; global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing) { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); } } }
即表示编译成功。
接下来我们要进行序列化和反序列化的工作
创建一个Unity工程。新建一个文件夹名为Plugins将ProGen文件夹下的protobuf-net.dll文件放入此文件夹。
然后将前面编译完成的Test.cs文件放入工程中。
目录结构如下所示
这里只是demo所以不创建Secens文件夹和Scripts文件夹了
在Unity工程中新建cs文件名为TestProbuffer。代码如下
using System.Collections; using System.Collections.Generic; using UnityEngine; using ProtoBuf; using System.IO; using proto.Test; public class TestProbuffer : MonoBehaviour { byte[] testByte; // Use this for initialization void Start () { ReqLogin reqLogin = new ReqLogin(); reqLogin.account = "oneSitDown"; reqLogin.password = "2222"; testByte = Serialize(reqLogin); } // Update is called once per frame void Update () { if(Input.GetKeyDown(KeyCode.Space)) { ReqLogin reqLogin = (ReqLogin)Deserialize<ReqLogin>(testByte); Debug.Log(reqLogin.account); Debug.Log(reqLogin.password); } } public byte[] Serialize(IExtensible msg) { byte[] result; using (var stream = new MemoryStream()) { Serializer.Serialize(stream, msg); result = stream.ToArray(); } return result; } public IExtensible Deserialize<IExtensible>(byte[] message) { IExtensible result; using (var stream = new MemoryStream(message)) { result = Serializer.Deserialize<IExtensible>(stream); } return result; } }
将脚本挂在场景的任意物体下,然后运行工程,按下空格键。会输出文字即表示操作成功。
工程的地址为 https://github.com/oneSitDown/probufMessage
相关文章推荐
- Unity使用protobuf-net进行二进制序列化与反序列化
- 使用protoBuf进行序列化和反序列化
- 【protobuf】 3.使用protobuf-csharp-port进行序列化和反序列化
- Beetle使用Protobuf.net进行对象序列化传输
- C#使用Protocol Buffer(ProtoBuf)进行对象的序列化与反序列化
- java序列化/反序列化之xstream、protobuf、protostuff 的比较与使用例子
- Unity跨iOS、Android平台使用protobuf-net的方法
- java序列化/反序列化之xstream、protobuf、protostuff 的比较与使用例子
- C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
- java序列化/反序列化之xml、protobuf、protostuff 的比较与使用例子
- Unity中使用Protobuf-net
- Protobuf-net在Unity中的序列化与反序列化
- .net MVC 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值
- Android下使用Protobuf进行序列化
- 序列化和反序列化及Protobuf 基本使用
- C#使用Json.Net进行序列化和反序列化及定制化
- Android下使用Protobuf进行序列化
- C#使用ProtocolBuffer(ProtoBuf)进行Unity中的Socket通信
- C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
- asp.net core 使用 Redis 和 Protobuf 进行 Session 缓存