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就崩了。
如果有使用上的问题,可以联系作者,互相探讨。