unity protobuf-netunity protobuf-net使用实践

1 下载代码
git clone https://github.com/protobuf-net/protobuf-net.git

2 本人使用vs2019编译发布
进入protobuf-net\src
打开protobuf-net.sln
全部重新生成 OK
如果有问题,一般可以补安装一些sdk。。。

实际使用到的是protobuf-net和protogen
在vs里选中这两个目录,右键生成,done

3 将protobuf-net 发布目录下的dll全部放到unity plugins下
将protogen exe也放过来

4 测试一下protogen,
.\protogen.exe --csharp_out=…/…/scripts/cs --package=TEST_PROTO proto/game.proto
pause

syntax="proto3";


message PRO_USER{
	optional int32 user_id = 1;
	optional int32 last_login_ts = 2;
}

message GameData{
	optional PRO_USER user = 1;
}

如果缺dll 就到发布目录下找找,把缺的也丢进来
可以在Assets\Plugins\protobuf-net\cs\proto 目录下看到cs已经生成
在这里插入图片描述

5 测试一下unity下的使用

using TEST_PROTO;
public GameData _db;

...
_db = new GameData();
if(_db != null)
    _db.User = new ProUser();
if (_db == null || _db.User == null)
    return;

...
 private void pack()
    {
        Debug.Log("begin pack");
        Debug.Log("find obj suc");
        if (_db != null && _db.User != null)
        {
            _db.User.UserId = 123;
            using (var file = File.Create("GameDB.bin"))
            {
                Serializer.Serialize(file, _db);
                Debug.Log("pack suc");
            }
        }
         
    }
    private void unpack()
    {
        Debug.Log("begin unpack");
        
        using (var file = File.OpenRead("GameDB.bin"))
        {
            _db = Serializer.Deserialize<TEST_PROTO.GameData>(file);
            if (_db != null && _db.User != null)
            {
                Debug.Log("unpack suc");
                Debug.Log(_db.User.UserId);
            }
        }
    }
    

output:
123
UnityEngine.Debug:Log(Object)

all done
接下来 就可以随意玩.proto了。

需要注意任何节点使用之前都尽量做非空判断,否则,一旦操作了空对象,unity就崩了。

如果有使用上的问题,可以联系作者,互相探讨。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值