protobuf_for_unity - unity3D 游戏引擎


GPL
跨平台
C#

软件简介

Google 的 protobuf 3.0.x,unity3D 游戏引擎。

protocolbuffer 是google 的一种数据交换的格式,它独立于语言,独立于平台。google
提供了多种语言的实现:java、c#、c++、go、python等,每一种实现都包含了相应语言的编译器以及库文件。

但官方提供的C#库文件
google/protobuf
需要.net4.5及以上版本,并不支持Unity游戏引擎。

为了在Unity中使用Protobuf,我对其源码进行了兼容性修改,希望能为有需求的人提供帮助。

使用方法:
将目录 Google.Protobuf 下的所有文件拷到 Unity 工程中。

接下来可以参考 test_msg.proto

syntax = "proto3";message TheMsg {
  string name = 1;
  int32 num = 2;
}

以及 Test.cs

void Start ()
{
    TheMsg msg = new TheMsg();
    msg.Name = "am the name";
    msg.Num = 32;

    Debug.Log(string.Format("The Msg is ( Name:{0},Num:{1} )",msg.Name,msg.Num));    byte[] bmsg;    using (MemoryStream ms = new MemoryStream())
    {
        msg.WriteTo(ms);
        bmsg = ms.ToArray();
    }


    TheMsg msg2 = TheMsg.Parser.ParseFrom(bmsg);
    Debug.Log(string.Format("The Msg2 is ( Name:{0},Num:{1} )",msg2.Name,msg2.Num));

}

测试输出: