Unity使用Protobuf

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);

运行结果

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

相关推荐
警醒与鞭策6 小时前
Direct3D 12 中的内存管理
3d·游戏引擎·direct3d
淡海水10 小时前
16-02-C#常用数据结构源码-附录B-源码索引速查
unity·c#·游戏引擎·il2cpp
速易达网络12 小时前
无人机远程操控仿真
游戏引擎·cocos2d
郝学胜-神的一滴12 小时前
游戏引擎原理与实践 02:揭开3A游戏背后的技术面纱
开发语言·计算机网络·程序人生·游戏·游戏引擎
警醒与鞭策21 小时前
【无标题】
android·unity·性能优化·游戏引擎·perforce
zhchyun20081 天前
【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(10)
ui·unity·游戏引擎
地狱为王1 天前
Unity使用NVIDIA的Audio2Face驱动口型
unity·游戏引擎
ebiobiz1 天前
STM32F4 Unity 单元测试与多次测试失败率统计详细步骤
stm32·unity·单元测试
一线灵1 天前
跨平台游戏引擎 Axmol-2.11.5 发布
游戏引擎·lua
淡海水1 天前
16-01-C#数据结构源码-附录A-术语表
unity·c#·游戏引擎·il2cpp