1.下载Protobuf ProtoBuf

2.打开它并且编译 如果有报错下载相应的.net版本即可

这里默认是6.0.100 由于我本机是8.0.100所以我改了这个文件

3.编译后的文件复制到Unity Assets/Plugins下

4.写个测试的proto文件

5.然后使用protoc生成 这里实现了一个简单的bat批量生成 Protos Codes 和protoc.exe在一个目录下 双击gen.bat即可

csharp
@echo off
rem 设置路径变量
set PROTOC_PATH="protoc.exe"
set PROTO_DIR="Protos"
set OUTPUT_DIR="Codes"
rem 检查目录是否存在
if not exist %PROTO_DIR% (
echo Error: protos directory does not exist.
exit /b
)
rem 创建输出目录
if not exist %OUTPUT_DIR% mkdir %OUTPUT_DIR%
rem 批量处理 .proto 文件
for %%f in (%PROTO_DIR%\*.proto) do (
%PROTOC_PATH% --proto_path=%PROTO_DIR% --csharp_out=%OUTPUT_DIR% %%f
)
echo Code generation complete.
测试例子
csharp
Monster monster = new Monster();
monster.Id = 100;
monster.Name = "虹魔教主";
byte[] buffer = monster.ToByteArray();
Monster ms = Monster.Parser.ParseFrom(buffer);
运行结果

具体的运行和设计根据项目而定吧 好像也没啥要写的了 哈哈哈