Unity中使用ProtoBuf-保姆式教程
·ProtoBuf介绍
ProtoBuf 是结构数据序列化方法,可简单类比于 XML、JSON,其具有以下特点:
- 语言无关、平台无关。即 ProtoBuf 支持 Java、C++、Python 等多种语言,支持多个平台
- 高效。即比 XML 更小(3 ~ 10倍)、更快(20 ~ 100倍)、更为简单
- 扩展性、兼容性好。你可以更新数据结构,而不影响和破坏原有的旧程序
·ProtoBuf获取
我这边选择的版本为 ProtoBuf 3.5.x
一共需要两个步骤
1.Google.Protobuf
从GitHub上获取完整版 传送门, 选择3.5.x的版本
这里有2条方式可供选择
(1)在unity中使用源码
Google.Protobuf这个文件夹直接放入到unity中
(2)在unity中使用Google.Protobuf.dll
使用vs打开它,用release去编译Google.Protobuf这个工程,在release这个目录下你会得到这个dll,放入到你的Libraries中
2.Protoc
从GitHub上获取windows版本 传送门, 选择3.5.1的版本
在bin中找到我们要的protoc.exe
写好 proto 文件之后用 protoc 编译器将 .proto文件编译成目标语言。
·.proto文件
测试文件内容
// 指定版本
syntax = "proto3";
// C#中的namespace
package ProtoTestoption optimize_for = SPEED;// java文件路径
option java_package = "com.montior.proto";// java文件名称
option java_outer_classname = "MonitorData";// 消息结果。
message MsgResult {// 结果码。int32 code = 1;// 错误消息。string err_msg = 2;}// 接收包
message TaskProtocol {// 数据类型int32 packType = 1;// 具体数据bytes content = 3;}// 包的类型
enum PackType {LOGIN = 0;CREATE_TASK = 2;DELETE_TASK = 3;
}message LoginPack{string username = 1;
}message LoginPack2{string username = 1;
}message CreateTaskPack{string taskId = 1;string taskName = 2;
}
message:消息类型,类似于一个类
package:包名,CSharp中的命名空间,用来防止不同消息类型的冲突
enum:枚举,这个需要我说吗?
option:选项,说明下我这边用到的
option java_package = “com.example.foo”;// java文件路径
option java_outer_classname = “Ponycopter”;// java文件名称
option optimize_for = SPEED;//可以被设置为 SPEED, CODE_SIZE,or LITE_RUNTIME。这些值将通过如下的方式影响C++及java代码的生成:注:以上选项,CSharp都用不着的,就是写着玩儿....
- 数据类型
protobuf 数据类型
描述
C++
bool
布尔类型
bool
double
64位浮点数
double
float
32为浮点数
float
int32
32位整数、
int
uin32
无符号32位整数
unsigned int
int64
64位整数
__int64
uint64
64为无符号整
unsigned __int64
sint32
32位整数,处理负数效率更高
int32
sing64
64位整数 处理负数效率更高
__int64
fixed32
32位无符号整数
unsigned int32
fixed64
64位无符号整数
unsigned __int64
sfixed32
32位整数、能以更高的效率处理负数
unsigned int32
sfixed64
64为整数
unsigned __int64
string
只能处理 ASCII字符
std::string
bytes
用于处理多字节的语言字符、如中文
std::string
- 关键字
指定字段 说明 required 表示是一个必须字段,必须相对于发送方,在发送消息之前必须设置该字段的值,对于接收方,必须能够识别该字段的意思。发送之前没有设置required字段或者无法识别required字段都会引发编解码异常,导致消息被丢弃。 optional 表示是一个可选字段,可选对于发送方,在发送消息时,可以有选择性的设置或者不设置该字段的值。对于接收方,如果能够识别可选字段就进行相应的处理,如果无法识别,则忽略该字段,消息中的其它字段正常处理。---因为optional字段的特性,很多接口在升级版本中都把后来添加的字段都统一的设置为optional字段,这样老的版本无需升级程序也可以正常的与新的软件进行通信,只不过新的字段无法识别而已,因为并不是每个节点都需要新的功能,因此可以做到按需升级和平滑过渡。 repeated 表示该字段可以包含0~N个元素。其特性和optional一样,但是每一次可以包含多个值。可以看作是在传递一个数组的值。
·在Unity的布局
UnityProject
-Asset
--Libraries
---Google.Protobuf // Protobuf源文件/dll文件
--Scripts/Editor/Proto2CSEditor // 放置.proto文件转化为cs的代码
--Scripts/ProtoMessage // 放置转化的cs的代码
-Proto
--monitorData.proto //放置.proto文件
--protoc.exe //
·如何使用protoc.exe把.proto文件转化为.cs
[MenuItem("Tools/Proto2CS")]public static void AllProto2CS(){string rootDir = Environment.CurrentDirectory;string protoDir = Path.Combine(rootDir, "Proto/");string protoc;if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){protoc = Path.Combine(protoDir, "protoc.exe");}else{protoc = Path.Combine(protoDir, "protoc");}string hotfixMessageCodePath = Path.Combine(rootDir, "Assets", "Scripts", "ProtoMessage/");string argument2 = $"--csharp_out=\"{hotfixMessageCodePath}\" --proto_path=\"{protoDir}\" monitorData.proto";Run(protoc, argument2, waitExit: true);UnityEngine.Debug.Log("proto2cs succeed!");AssetDatabase.Refresh();}public static Process Run(string exe, string arguments, string workingDirectory = ".", bool waitExit = false){try{bool redirectStandardOutput = true;bool redirectStandardError = true;bool useShellExecute = false;if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){redirectStandardOutput = false;redirectStandardError = false;useShellExecute = true;}if (waitExit){redirectStandardOutput = true;redirectStandardError = true;useShellExecute = false;}ProcessStartInfo info = new ProcessStartInfo{FileName = exe,Arguments = arguments,CreateNoWindow = true,UseShellExecute = useShellExecute,WorkingDirectory = workingDirectory,RedirectStandardOutput = redirectStandardOutput,RedirectStandardError = redirectStandardError,};Process process = Process.Start(info);if (waitExit){process.WaitForExit();if (process.ExitCode != 0){throw new Exception($"{process.StandardOutput.ReadToEnd()} {process.StandardError.ReadToEnd()}");}}return process;}catch (Exception e){throw new Exception($"dir: {Path.GetFullPath(workingDirectory)}, command: {exe} {arguments}", e);}}
·序列化与反序列化
·举个栗子
MsgResult result = new MsgResult{Code = -999,ErrMsg = "Error"};TaskProtocol msgResult = new TaskProtocol{PackType = 111,Content = result.ToByteString()};byte[] s = packer.SerializeTo(msgResult);Debug.Log("---------------------------------------------------");TaskProtocol response = new TaskProtocol();packer.DeserializeFrom(response, s);MsgResult responseMsgResult = new MsgResult();packer.DeserializeFrom(responseMsgResult, response.Content.ToByteArray());Debug.Log(response.PackType);Debug.Log(responseMsgResult.Code);Debug.Log(responseMsgResult.ErrMsg);
·Unity中还可以使用protobuf-net.dll
<!--暂无-->
·结语
如果这样你都还配置不好ProtoBuf,不知道怎么序列化和反序列化的话!!!!
那就多看几遍,哈哈哈
Unity中使用ProtoBuf-保姆式教程相关推荐
- Node.js的完全卸载与下载安装及各种npm、nvm、nrm配置(保姆式教程---提供全套安装包)---node.js的安装与配置(0)
Node.js的完全卸载与下载安装及各种npm.nvm.nrm配置(保姆式教程-提供全套安装包)-node.js的安装与配置(0) node的卸载 1.打开控制面板 我的电脑右键--->属性-- ...
- Node.js下载安装及各种npm、cnpm、nvm、nrm配置(保姆式教程—提供全套安装包)—cnpm的安装与配置(3)
Node.js下载安装及各种npm.cnpm.nvm.nrm配置(保姆式教程-提供全套安装包)-npm的安装与配置(2) 四.安装cnpm 1.管理员身份运行cmd,输入如下命令 npm instal ...
- Node.js下载安装及各种npm、cnpm、nvm、nrm配置(保姆式教程---提供全套安装包)---npm的安装与配置(2)
Node.js下载安装及各种npm.cnpm.nvm.nrm配置(保姆式教程-提供全套安装包)-node.js的安装与配置(1) 三.配置npm安装的全局模块 需要配置的进行配置(不用C盘的配置,用C ...
- Xilinx FPGA平台DDR3设计保姆式教程(3)MIG IP核使用教程及DDR读写时序
干货来了,用DDR搬砖,只需要会用IP就好,Xilinx官方YYDS! ---------------------------------------------------------------- ...
- EasyMonitor 视频监控开发平台 安装教程(保姆式教程)
~~ EasyMonitor 视频监控开发平台 安装教程(保姆式教程) 本教程基于虚拟机环境测试 一.虚拟机安装CENTOS7 我用的是MINI版的只有900M,附一个网盘链接: https://pa ...
- 在Oracle Linux上部署Yunzai Bot v3保姆式教程/甲骨文云/云崽Bot/原神
去我的博客查看本文:在Oracle Linux上部署Yunzai Bot v3保姆式教程 – 肚 (iocky.com) 本文也在Github与gitee可用. 初始配置 直接注册最低配置的就ok了, ...
- Xilinx FPGA平台DDR3设计保姆式教程(1)DDR3基础简介
如果我们只是拿来用ddr搬砖,那么它就简单,知道IP怎么使用就好,但是要想知其所以然,理论知识是必备的,这也是我们初学者所欠缺的东西,慢慢修炼吧! 汇总篇: Xilinx平台DDR3设计保姆式教程(汇 ...
- unity项目源码_在Unity中使用protobuf
Protocol Buffers (通常简称为protobuf) 是Google开发的一种格式,这种格式与开发语言无关.与运行平台无关,用于序列化结构数据,并且很容易扩展.这种格式可以用于通信协议.数 ...
- Node.js下载安装及各种npm、cnpm、nvm、nrm配置(保姆式教程—提供全套安装包)—nrm的安装与配置(5)
Node.js下载安装及各种npm.cnpm.nvm.nrm配置(保姆式教程-提供全套安装包)-nvm的安装与配置(4) 一.nrm安装与使用 1.管理员运行cmd,输入如下,全局安装nrm: npm ...
最新文章
- 第 十 天 : 添 加 硬 盘 和 分 区 挂 载 等
- Jasypt : 整合spring boot加密应用配置文件敏感信息
- 手下两个应届生,一个踏实喜欢加班,一个技术强挑活,怎么选?
- 条款9:不要在构造和析构过程中调用virtual函数
- python 文件操作 os.mkdir()函数
- SAP Spartacus 如何根据 page layout 获得对应支持的 slots
- amd核芯显卡控制面板自定义分辨率_主流显卡的一位猛将:蓝宝石Radeon RX 5500XT显卡首测...
- security中的@EnableGlobalMethodSecurity注解详解
- php实例化类的方法,在PHP中实例化类实例的不同方法
- python 升级服务器_开发服务器之升级到Python2.7
- JavaEE 注解笔记:(一)
- Linux杀毒软件ClamAV初次体验
- Wampserver_开启CURL
- 班级抽签小程序——项目总结
- springmvc/ssm框架详细图文解说流程图及运行原理_附源码
- 酒店客房管理系统JAVA-SSM-MYSQL
- 机器学习基础补习04---凸优化
- 记录List做分页、模糊搜索、时间排序
- 有便宜又好用的云服务器推荐?
- USACO 3.2 Magic Squares 魔板 (BFS-HASH)