protobuf_for_unity 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
protobuf_for_unity 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
protobuf_for_unity 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 GPL
开发语言 C#
操作系统 跨平台
软件类型 开源软件
开源组织
地区 国产
投 递 者 贡法新
适用人群 未知
收录时间 2016-09-19

软件简介

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));

}

测试输出:


展开阅读全文

代码

评论

点击加入讨论🔥(3) 发布并加入讨论🔥
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
3 评论
21 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部