您的位置:首页 > 移动开发 > Unity3D

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: