注意:此方法不适用于DLL,需另外增加步骤,因为unity客户端只支持.NET3.5,而如果DLL中需要使用到用protobuf中的类,那么就需要在DLL中引入protubuf-net的DLL,那么导入unity的时候这些类将不再能使用,或者说1个类会出现2个程序集的定义.各种解压也就没用了,除非你的DLL中不需要使用这个消息类.
构想(还没验证):将protobuf-net的源码引入dll中,然后再将DLL引入unity中.应该可以正常使用.找个机会试试先

 

1、下载protobuf-net

 

2、创建Unity工程,创建一个Plugins文件夹,将protobuf-net解压把里面得protobuf-net放到Plugins

Unity3D protobuf-net使用方式 (源码方式)

 

3、创建一个名为mcs的文本文件,里面写上-unsafe

Unity3D protobuf-net使用方式 (源码方式)

 

4、重启Unity

 

5、编译自动生成cs代码工具

Unity3D protobuf-net使用方式 (源码方式)

 

protogen.exe就是刚才生成的

Unity3D protobuf-net使用方式 (源码方式)

 

 

6、编写.proto文件

Unity3D protobuf-net使用方式 (源码方式)

 

message.proto里写入

1
2
3
4
5
6
7
8
9
10
message TeamCharacterOne
{
    required    uint64              CharacterId             = 1;
    required   string              CharacterName           = 2;
    required    int32               RoleId                  = 3;
    required    int32               Level                   = 4;
    required    int32               Ladder                  = 5;
    required    int32               FightPoint              = 6;
    optional    int32               QueueResult             = 7;   
}

  

7、 生成.cs代码

创建一个proto.bat文件文件

里面写入

@echo off  
rem 查找文件  
for /f "delims=" %%i in ('dir /b ".\*.proto"') do echo %%i  
rem 转cpp  for /f "delims=" %%i in ('dir /b/a "*.proto"') do protoc -I=. --cpp_out=. %%i  
for /f "delims=" %%i in ('dir /b/a "*.proto"') do protogen -i:%%i -o:%%~ni.cs  
pause

 

8、把代码放入Unity工程

相关文章:

  • 2021-07-20
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
  • 2022-02-03
猜你喜欢
  • 2021-08-25
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
相关资源
相似解决方案