unity中简单实用protobuf(Protobuf-net)

来源:互联网 发布:google pixel手机 知乎 编辑:程序博客网 时间:2024/06/05 04:18

(一)

Protobuf 是Google的一个开源序列化库,因为使用的数据压缩算法等优化,序列化的数据较Xml更小,速度更快,因为序列化后数据是以紧凑的二进制流形式展现的,所以几乎不可直接查看。

由于Protobuf不支持.Net3.5及以下版本,所以如果要在Unity3D当中使用,则需要用到第三方的Protobuf-net库。

Protobuf-net也是开源的,项目地址如下:https://github.com/mgravell/protobuf-net

本片文章介绍最简单其最简单的用法,其他用法见后面几篇。

  1. 创建一个C#的控制台程序
  2. 点击项目右键打开“管理NuGet程序包”。
  3. 搜索“Protobuf-net”,并安装,如下:

    这里写图片描述

  4. 编辑测试代码如下:

using System.Collections.Generic;using System.IO;namespace ProtoTest_1{    [ProtoBuf.ProtoContract]    class MyClass    {        [ProtoBuf.ProtoMember(1)]        public int _nNumber;        [ProtoBuf.ProtoMember(2)]        public string _strName;        [ProtoBuf.ProtoMember(3)]        public List<string> _lstInfo;        [ProtoBuf.ProtoMember(4)]        public Dictionary<int, string> _dictInfo;    }    class Program    {        static void Main(string[] args)        {            //测试用数据            MyClass my = new MyClass();            my._nNumber = 0;            my._strName = "test";            my._lstInfo = new List<string>();            my._lstInfo.Add("a");            my._lstInfo.Add("b");            my._lstInfo.Add("c");            my._dictInfo = new Dictionary<int, string>();            my._dictInfo.Add(1, "a");            my._dictInfo.Add(2, "b");            my._dictInfo.Add(3, "c");            using (FileStream stream = File.OpenWrite("test.dat"))            {                //序列化后的数据存入文件                ProtoBuf.Serializer.Serialize<MyClass>(stream, my);            }            MyClass my2 = null;            using (FileStream stream = File.OpenRead("test.dat"))            {                //从文件中读取数据并反序列化                my2 = ProtoBuf.Serializer.Deserialize<MyClass>(stream);            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

以上程序实现了MyClass类的序列化与反序列化操作,是不是很简单!

需要注意的是序列化类的类名前需要加上“[ProtoBuf.ProtoContract]”,然后每个字段需要按照顺序在前面加上“[ProtoBuf.ProtoMember(Index)]”,这样才能使用。 
后面将继续讲解protobuf-net的使用动态链接库和直接使用源码的这两种方式。


(二)

上一节使用的是NuGet程序包的方式,在程序中简单的使用Protobuf-net,接下来换一种方式。 
使用源码编译后的动态链接库,这样有个好处就是,你可以选择目标平台。 
首先需要下载源码:https://github.com/mgravell/protobuf-net

可以使用Git克隆项目,也可以下载压缩包,本人使用的是git方式。 
源码获取后,打开源码目录下的“Proto 2013.sln”工程文件打开项目,本人使用的是Vs2015。 
只需要关注三个工程即可: 
1. protobuf-net:核心工程,生成后的dll就是上一节中使用NuGet程序包的方式导入的dll,用于序列化与反序列化等操作。 
2. protogen:用于将标准的protobuf定义文件“ * .proto”转换成“ * .cs”文件,这样就免去了重新定义协议。 
3. precompile:用于生成protogen生成的文件所生成的dll所对应的序列化与反序列化dll。

这里写图片描述

因为要在Unity中使用,所以生成配置调整到unity且为AnyCpu。然后生成如上三个工程,将生成的文件全部拷贝出来根据工程重命名一下,如下:

这里写图片描述

至此,所需的工具就准备完成。 
接下来在将讲解在Unity中如何使用它们。

第一步,使用protogen将“.proto”定义文件生成对应的“.cs”文件。

直接使用protogen自带的“descriptor.proto”文件。 
执行以下命令:

//将decriptor.proto文件转换成decriptor.cs文件,且命名空间为MyProto.\protogen.exe -i:descriptor.proto -o:descriptor.cs -ns:MyProto
  • 1
  • 2
  • 1
  • 2

第二步,创建动态链接库工程,将decriptor.cs生成对应的decriptor.dll动态链接库。

本人使用的是MonoDevelop,当然也可以使用VS(.Net 3.5以下)。 
创建名为“descriptor”动态链接库工程,删除工程创建时自动生成的.cs文件,然后将“descriptor.cs”导入到工程当中,并且引用“protobuf-net”工程生成的“protobuf-net.dll”动态链接库。 
需要注意的是,只能生成Release版,且需要允许不安全代码。本人设置的目标框架是“Mono/.NET2.0”。 
启动生成工程就得到了decriptor.dll动态链接库。

第三步,使用precompile生成decriptor.dll对应的序列化与反序列化的“descriptor.serializer.dll”动态链接库。

将生成的decriptor.dll与protobuf-net.dll放在同一个文件夹下。 
执行以下命令:

//生成 descriptor.dll对应的descriptor.serializer.dll,且命名空间为MyProto.Serializer.\precompile\precompile.exe -o:descriptor.serializer.dll -t:MyProto.Serializer descriptor.dll
  • 1
  • 2
  • 1
  • 2

至此Unity工程所需要的文件就准备好了。如果对工具的命令有不懂的地方,可以直接再后面加上“/?”获得帮助。

  • protobuf-net.dll
  • descriptor.dll
  • descriptor.serializer.dll

接下来就是在Unity工程当中的使用方法。

创建一个Unity工程,将三个dll都导入到工程中,然后创建一个脚本并附加在摄像机上,脚本代码如下:

using UnityEngine;using google.protobuf;using System.IO;using ProtoBuf.Meta;public class test : MonoBehaviour{    void Start()    {        //创建一个序列化反序列化对象        RuntimeTypeModel ser = MyProto.Serializer.Create();        //实例化一个需要序列化的对象        DescriptorProto my = new DescriptorProto();        my.name = "XiangMu";        //将序列化后的数据写入文件        using (Stream s = File.OpenWrite("test.dat"))        {            ser.Serialize(s, my);        }        DescriptorProto my2 = null;        //从文件中读取并反序列化到对象        using (Stream s = File.OpenRead("test.dat"))        {            my2 = ser.Deserialize(s, my2, typeof(DescriptorProto)) as DescriptorProto;            //打印            print(my2.name);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

使用动态链接库的方法至此就讲解完了,下一章将讲解如何直接使用源码的方式在Unity中使用protobuf-net。


(三)

前两篇讲解了如何使用导入NuGet程序包和动态链接库的方式来使用Protobuf-net。接下来将讲解如何直接在Unity中使用源码来进行序列化与反序列化操作。

首先需要获取源码,获取方式上一篇已经说明,不清楚的可以看:[Unity3D]简单使用Protobuf-net(二)

创建一个Unity工程,然后将源码中的“protobuf-net”文件夹导入到Unity工程中,“protobuf-net”文件夹是“protobuf-net”的工程目录,包含了其所有的需要用到的源码。等待Unity编译完成。 
经过Unity编译后会报不安全代码的错误,是因为使用了指针,则需要在Assets目录下添加一个“smcs.rsp”文件,用于控制smcs的脚本编译。 
在“smcs.rsp”中添加如下内容:

-unsafe-define:FEAT_COMPILER;PLAT_BINARYFORMATTER;PLAT_XMLSERIALIZER;PLAT_NO_INTERLOCKED;FEAT_SAFE
  • 1
  • 2
  • 1
  • 2

-unsafe 表示允许不安全代码; 
-define 定义宏,用于控制需要编译的代码。因为是在Unity中使用,就按如上定义。不清楚如何定义的可以看“protobuf-net”工程目录下的“protobuf-net.csproj”文件,如下: 
这里写图片描述

如果还想添加其他命令,可以通过以下方式查看:

< Unity安装目录 > \Editor\Data\Mono\lib\mono\unity\smcs.exe -help
  • 1
  • 1

“smcs.rsp”文件修改保存后,必须需要重新编译文件,可以将其Reimport。此时,不安全代码的错误就没有了。

至此Protobuf-net导入Unity工程成功,接下来讲解如何使用。 
使用的方法与[Unity3D]简单使用Protobuf-net(一)中的是用方式是一样的。

需要序列化与反序列化的对象,可以手动定义,也可以通过“ *.proto“文件来生成。通过文件生成的方法[Unity3D]简单使用Protobuf-net(二)中已经讲解,不清楚的可以去看看protogen是怎么使用的,在此就不赘述。

在摄像机上附加一个脚本,并添加如下代码:

using UnityEngine;using System.Collections;using System.IO;//定义一个序列化与反序列化对象[ProtoBuf.ProtoContract]class Person{    [ProtoBuf.ProtoMember(1)]    public string name;    [ProtoBuf.ProtoMember(2)]    public int age;}public class test : MonoBehaviour{    void Start()    {        Person per = new Person();        per.age = 1;        per.name = "xiangmu";        using (Stream s = File.OpenWrite("test.dat"))        {            //序列化对象到文件            ProtoBuf.Serializer.Serialize<Person>(s, per);        }        Person per2 = null;        using (Stream s = File.OpenRead("test.dat"))        {            //从文件中读取并反序列化到对象            per2 = ProtoBuf.Serializer.Deserialize<Person>(s);            //打印            print("name>" + per2.name + " age>" + per2.age);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

Protobuf-net的简单使用就这些,如果后续有高级用法,本人会继续更新。


0 0