当前位置 : 主页 > 大数据 > 区块链 >

Unity中Protobuf-net的使用

来源:互联网 收集:自由互联 发布时间:2021-06-22
protobuf的使用方式 1.手动编写 2..proto文件自动生成 1.手动方式 手动编写protobuf序列化的实体类 [ProtoContract] //序列化类 public class Student { [ProtoMember( 1 )] //序列化属性 public string name; [Proto

protobuf的使用方式

  • 1.手动编写
  • 2..proto文件自动生成

1.手动方式

手动编写protobuf序列化的实体类

[ProtoContract]//序列化类
    public class Student
    {
        [ProtoMember(1)]//序列化属性
        public string name;
        [ProtoMember(2)]
        public string sex;
        [ProtoMember(3)]
        public Address address;

        public Student(){}
        public Student(string name, string sex, Address address)
        {
        this.name = name;
        this.sex = sex;
        this.address = address;
        }
    }
    [ProtoContract]//序列化类
    public class Address
    {
        [ProtoMember(1)]//序列化属性
        public string province;
        [ProtoMember(2)]
        public string city;
        [ProtoMember(3)]
        public string street;

        public Address()
        { }
        public Address(string province, string city, string street)
        {
            this.province = province;
            this.city = city;
            this.street = street;
        }
    }
###protobuf序列化和反序列化
    public TestProtobuf()
    {
        //protobuf序列化
        Student st = new Student("小明", "男", new Address("河南", "郑州", "东三街"));
        MemoryStream ms = new MemoryStream();
        Serializer.Serialize<Student>(ms, st);//序列化

        string result = Encoding.UTF8.GetString(ms.ToArray());
        Console.WriteLine(result);
        ms.Close();

        //protobuf反序列化
        byte[] buffer = Encoding.UTF8.GetBytes(result);
        MemoryStream mem = new MemoryStream(buffer);
        Student student = Serializer.Deserialize<Student>(mem);//反序列化
        mem.Close();
        //学生数据解析成功
    }

2.proto文件方式

1.生成protobuf-net.dll库和protogen.exe工具

  • 下载github上的protobuf-net工程地址:[https://github.com/mgravell/protobuf-net]
  • 使用vs打开.csproj文件,然后关闭保存,生成.sln文件。
  • 使用vs打开.sln文件。
  • 使用Nuget下载protobuf-net库。命令 : Install-Package protobuf-net
  • 配置管理器为Release,然后生成解决方案。
  • 在工程的bin/Release下,找到我们生成的文件,包含一个叫protogen.exe的文件(其他的文件也都有用,不要删除)

2.编译.proto文件

  • 编写 percen.proto文件
package information            // 定义了package的名字(命名空间)
    message Person {               //定义message名(类名) 
      required string name = 1; //必须赋值的字段
      optional int32 id = 2;    //可以不赋值的字段
      repeated string email = 3;//可重复字段做List使用

      enum PhoneType {            //枚举类型
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
      }

      message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];
      }

      repeated PhoneNumber phone = 4;
    }
  • cmd命令行下编译.proto文件
  • 把percen.proto放到protogen.exe的同级目录下。
  • cd 到protogen.exe的Release目录下。
  • 输入:protogen -i:percen.proto -o:Person.cs 生成percen.cs文件
  • 然后把percen.cs拷贝到unity项目中就可以使用了,用法跟手动一样

  • percen.cs文件
//------------------------------------------------------------------------------
    // <auto-generated>
    // This code was generated by a tool.
    //
    // Changes to this file may cause incorrect behavior and will be lost if
    // the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------

    // Generated from: percen.proto
    namespace information
    {
      [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Person")]
      public partial class Person : global::ProtoBuf.IExtensible
      {
        public Person() {}

        private string _name;
        [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"name", DataFormat = global::ProtoBuf.DataFormat.Default)]
        public string name
        {
          get { return _name; }
          set { _name = value; }
        }
        private int _id = default(int);
        [global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
        [global::System.ComponentModel.DefaultValue(default(int))]
        public int id
        {
          get { return _id; }
          set { _id = value; }
        }
        private readonly global::System.Collections.Generic.List<string> _email = new global::System.Collections.Generic.List<string>();
        [global::ProtoBuf.ProtoMember(3, Name=@"email", DataFormat = global::ProtoBuf.DataFormat.Default)]
        public global::System.Collections.Generic.List<string> email
        {
          get { return _email; }
        }

        private readonly global::System.Collections.Generic.List<information.Person.PhoneNumber> _phone = new global::System.Collections.Generic.List<information.Person.PhoneNumber>();
        [global::ProtoBuf.ProtoMember(4, Name=@"phone", DataFormat = global::ProtoBuf.DataFormat.Default)]
        public global::System.Collections.Generic.List<information.Person.PhoneNumber> phone
        {
          get { return _phone; }
        }

      [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"PhoneNumber")]
      public partial class PhoneNumber : global::ProtoBuf.IExtensible
      {
        public PhoneNumber() {}

        private string _number;
        [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"number", DataFormat = global::ProtoBuf.DataFormat.Default)]
        public string number
        {
          get { return _number; }
          set { _number = value; }
        }
        private information.Person.PhoneType _type = information.Person.PhoneType.HOME;
        [global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"type", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
        [global::System.ComponentModel.DefaultValue(information.Person.PhoneType.HOME)]
        public information.Person.PhoneType type
        {
          get { return _type; }
          set { _type = value; }
        }
        private global::ProtoBuf.IExtension extensionObject;
        global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
          { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
      }

        [global::ProtoBuf.ProtoContract(Name=@"PhoneType")]
        public enum PhoneType
        {

          [global::ProtoBuf.ProtoEnum(Name=@"MOBILE", Value=0)]
          MOBILE = 0,

          [global::ProtoBuf.ProtoEnum(Name=@"HOME", Value=1)]
          HOME = 1,

          [global::ProtoBuf.ProtoEnum(Name=@"WORK", Value=2)]
          WORK = 2
        }

        private global::ProtoBuf.IExtension extensionObject;
        global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
          { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
      }

    }
网友评论