この記事のいるProtobufを使用してのいいところはいるProtobufあなたはUnity3Dを使用したい場合ので、あなたは第三者いるProtobufネットライブラリを使用する必要があり、以下の.Net3.5をサポートしていません考慮して、導入されることはありません。
まず、githubのhttps://github.com/mgravell/protobuf-netからソースコードをダウンロード
そして、次のディレクトリの下に入力します。
、.csprojクリックしてプロジェクトにコンパイルし、次のチャートを入手:
いるProtobuf-net.dllに追加
ディレクトリ内の.csprojを開き、プロジェクトにそれをコンパイルし、次のディレクトリを入力します。
ディレクトリ内のファイルを持っている.Proto、私たちの目的はの.csファイルに.protoファイルにあります
バッチを作成するために、バッチ処理を書くxx.Bat
コンテンツ:
オフ@echo
REMファイルを探します
"delims =" %% I IN( 'DIR / B "\ *。プロト"')、F /用行うエコー%% I
REM转 "delims =" %% I IN( 'DIR / B / "* .proto"')DO protoc -I = F /ためのCPP。--cpp_out =。%%私
%% I -o::( 'DIR / B / "* .proto"')を行うprotogen -i中のため/ F "delims =" %%私は%%〜ni.cs
休止
その後xx.Batは.proto .csファイルにバッチファイルでフォルダを提出することができますクリックして、そのフォルダが生成したする.csファイルを見ることができます
今の書き込みaを聞かせて
message Person {
required string name=1;
required int32 id=2;
optional string email=3;
enum PhoneType {
MOBILE=0;
HOME=1;
WORK=2;
}
message PhoneNumber {
required string number=1;
optional PhoneType type=2 [default=HOME];
}
repeated PhoneNumber phone=4;
}
requied是必须有的字段、optional是可有可无的字段、repeated是可以重复的字段(数组或列表),同时枚举字段都必须给出默认值。
然后点击xx.Bat 就生成了我们想要的
//------------------------------------------------------------------------------
// <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: msg.proto
namespace msg
{
[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;
[global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
public int id
{
get { return _id; }
set { _id = value; }
}
private string _email = "";
[global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"email", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue("")]
public string email
{
get { return _email; }
set { _email = value; }
}
private readonly global::System.Collections.Generic.List<Person.PhoneNumber> _phone = new global::System.Collections.Generic.List<Person.PhoneNumber>();
[global::ProtoBuf.ProtoMember(4, Name=@"phone", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<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 Person.PhoneType _type = Person.PhoneType.HOME;
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"type", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
[global::System.ComponentModel.DefaultValue(Person.PhoneType.HOME)]
public 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); }
}
}
接下来要在Unity中应用 还应该把protobuf-net的源码放入Plugins
接着在Asset目录下创建smcs.rsp
内容为:
-unsafe
然后把你生成的.cs文件放入 就OK了