Unity中使用Protobuf3.0

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qweewqpkn/article/details/83098633

 今天准备在unity中使用protobuf, 但是遇到了一些问题,记录一下。

首先,我们到google protobuf github 上下载工程,主要有两个东西:

1.Protocol Compiler Installation

下载地址:https://repo1.maven.org/maven2/com/google/protobuf/protoc/

作用:这个是将我们.proto文件生成c#文件的exe

我下载了这个:

我放到了目录:D:\Library\Protobuf\protoc-3.6.1-win32\ 下,然后将这个目录加入到电脑的环境变量的Path中。

Proto目录结构:

上面的那个.bat内容如下,用于将Proto目录下的proto文件生成c#文件:

@echo off
cd Proto
set client_dest_path="..\..\..\Assets\Network\Protobuf"
set server_dest_path="..\..\TestServer"
for %%i in (*.*) do protoc --csharp_out=%client_dest_path% %%i
for %%i in (*.*) do protoc --csharp_out=%server_dest_path% %%i
echo success
pause

 

2.Protobuf Runtime Installation

(这个是运行时库,因为我们要进行序列化和反序列化要用到的)

下载地址:https://github.com/protocolbuffers/protobuf 

下载后,我们找到目录protobuf-3.6.1\csharp\src下的Google.Protobuf.sln工程用vs2017打开,然后可以看到如图的工程:

我们编译红框选中的工程就可以了,然后就会在目录:protobuf-3.6.1\csharp\src\Google.Protobuf\bin\Debug\net45

中生成Google.Protobuf.dll这个运行时dll,这个就是运行时库。

3.导入到unity

(1)将proto文件对应生成的c#文件放入到unity工程中

(2)将上面的Google.Protobuf.dll放入到unity工程中

恭喜你,你以为成功了吗?哈哈哈,并没有,错误来了,如图:

第一个错误:

意思就是unity不支持net4.5的库,只支持net3.5的库,于是在网上搜寻一圈,发现了这个:

https://github.com/bitcraftCoLtd/protobuf3-for-unity 

这个就是unity对应的net3.5的protobuf版本,于是下载下来呗,我打开工程后发现所有子工程都加载失败了(如果你能正常加载工程那么可以不管这个),原来是我没有下载.net core对应的库,地址是:https://www.microsoft.com/net/download

然后就能正常编译工程了,编译后将对应的net3.5版本的Google.Protobuf.dll放到unity中即可。

第二个错误:

意思就是我们的net3.5版本的运行时库中没有这个UnknownFieldSet, 这时想起我们使用的是3.6.1的proto编译器生成proto对应的c#文件,版本太高了,于是我降到了3.4.0的proto编译器,重新生成proto对应的c#文件,此时这个错误也就消失了。

so,踩坑完毕~~

猜你喜欢

转载自blog.csdn.net/qweewqpkn/article/details/83098633