Unity C# 网络学习(十二)——Protobuf生成协议
Unity C# 网络学习(十二)——Protobuf生成协议
一.安装
- 去Protobuf官网下载对应操作系统的protoc,用于将.proto文件生成对应语言的协议语言文件
- 由于我使用的是C#所以可以使用提供的C#的序列化反序列化的项目,然后自己编译出DLL放入Unity中使用
二.Protobuf 配置的规则(.proto文件的语法)
syntax = "proto3";//决定了proto文档的版本号
package GamePlayerTest;//命名空间import "test2.proto";//消息类
message TestMsg1 {//成员类型 成员名称 唯一编号//浮点数float testF = 1;double testD = 2;//变长编码//Protobuf会自动优化,可以尽量少的使用字节数,来存储内容int32 testInt32 = 3; //不太适用于负数int64 testInt64 = 4;//更适用于负数sint32 testSInt32 = 5;sint64 testSInt64 = 6;//无符号变长编码uint32 testUInt32 = 7;uint64 testUInt64 = 8;//固定字节数类型fixed32 testFixed32 = 9; //通常用于表示大于2的28次方的数 uintfixed64 testFixed64 = 10; //通常用于表示大于2的56次方的数 ulongsfixed32 testSFixed32 = 11; //intsfixed64 testSFixed64 = 12; //long//数组repeated int32 arr_int32 = 13;repeated string arr_string = 14;//字典map<int32,string> map1 = 15;//枚举TestEnum1 test_enum1 = 16;//嵌套消息message TestMsg2{int32 test_int32 = 1;}TestMsg2 test_msg2 = 17;//嵌套枚举enum TestEnum2{NORMAL = 0;BOSS = 1;}TestEnum2 test_enum2 = 18;GameSystemTest.HeartMsg heart_msg = 19;
}enum TestEnum1{NORMAL = 0;BOSS = 5;
}
syntax = "proto3";
package GameSystemTest;message HeartMsg{int64 time = 1;
}
三.生成对应的C#代码
- 打开cmd窗口
- 进入protoc.exe所在文件夹(也可以直接拖入到cmd中)
- 输入转换指令
- protoc.exe -I=配置路径 =csharp_out=输出路径 配置文件名
四.封装快捷生成协议文件
public static class GenerateProtobuf
{private const string ProtocPathExe = @"D:\Unity_Project\AgainLearnNet\Protobuf\protoc.exe";private const string ProtoPath = @"D:\Unity_Project\AgainLearnNet\Protobuf\proto";private const string OutPath = @"D:\Unity_Project\AgainLearnNet\Protobuf\csharp";[MenuItem("Protobuf/GenerateCSharp")]private static void GenerateCSharp(){DirectoryInfo directoryInfo = new DirectoryInfo(ProtoPath);FileInfo[] fileInfos = directoryInfo.GetFiles();foreach (var fileInfo in fileInfos){if(fileInfo.Extension != ".proto")continue;Process cmd = new Process();cmd.StartInfo.FileName = ProtocPathExe;cmd.StartInfo.Arguments = $"-I={ProtoPath} --csharp_out={OutPath} {fileInfo.Name}";cmd.Start();}}
}
五.协议的序列化和反序列化
1.文本流
private void Start(){MyTestMsg myTestMsg = new MyTestMsg();myTestMsg.PlayerId = 1;myTestMsg.Name = "zzs";myTestMsg.Friends.Add("wy");myTestMsg.Friends.Add("pnb");myTestMsg.Friends.Add("lzq");myTestMsg.Map.Add(1,"ywj");myTestMsg.Map.Add(2,"zzs");string path = Application.persistentDataPath + "/testMsg.msg";using (FileStream fs = new FileStream(path,FileMode.Create)){myTestMsg.WriteTo(fs);}MyTestMsg newMyTestMsg;using (FileStream fs = new FileStream(path,FileMode.Open)){newMyTestMsg = MyTestMsg.Parser.ParseFrom(fs);}Debug.Log(newMyTestMsg.PlayerId);Debug.Log(newMyTestMsg.Name);Debug.Log(newMyTestMsg.Friends.Count);Debug.Log(newMyTestMsg.Map[1]);Debug.Log(newMyTestMsg.Map[2]);}
2.内存流
private void Start(){MyTestMsg myTestMsg = new MyTestMsg{PlayerId = 1,Name = "zzs"};myTestMsg.Friends.Add("wy");myTestMsg.Friends.Add("pnb");myTestMsg.Friends.Add("lzq");myTestMsg.Map.Add(1,"ywj");myTestMsg.Map.Add(2,"zzs");byte[] buffer;using (MemoryStream ms = new MemoryStream()){myTestMsg.WriteTo(ms);buffer = ms.ToArray();}MyTestMsg newMyTestMsg;using (MemoryStream ms = new MemoryStream(buffer)){newMyTestMsg = MyTestMsg.Parser.ParseFrom(ms);}Debug.Log(newMyTestMsg.PlayerId);Debug.Log(newMyTestMsg.Name);Debug.Log(newMyTestMsg.Friends.Count);Debug.Log(newMyTestMsg.Map[1]);Debug.Log(newMyTestMsg.Map[2]);}
六.Protobuf的序列化和反序列化(优化调用方式)
private void Start(){MyTestMsg myTestMsg = new MyTestMsg{PlayerId = 1,Name = "zzs"};myTestMsg.Friends.Add("wy");myTestMsg.Friends.Add("pnb");myTestMsg.Friends.Add("lzq");myTestMsg.Map.Add(1,"ywj");myTestMsg.Map.Add(2,"zzs");byte[] buffer = myTestMsg.ToByteArray();MyTestMsg newMyTestMsg = MyTestMsg.Parser.ParseFrom(buffer);Debug.Log(newMyTestMsg.PlayerId);Debug.Log(newMyTestMsg.Name);Debug.Log(newMyTestMsg.Friends.Count);Debug.Log(newMyTestMsg.Map[1]);Debug.Log(newMyTestMsg.Map[2]);}
Unity C# 网络学习(十二)——Protobuf生成协议相关推荐
- Unity C# 网络学习(十一)——自定义协议生成工具
Unity C# 网络学习(十一)--自定义协议生成工具 在开发网络游戏中,协议是必不可少的东西,一款游戏可能有非常多的协议,但是协议的重复性非常高,而且前端后端都需要,人工完成显然不现实,可以通过共 ...
- PyTorch框架学习十二——损失函数
PyTorch框架学习十二--损失函数 一.损失函数的作用 二.18种常见损失函数简述 1.L1Loss(MAE) 2.MSELoss 3.SmoothL1Loss 4.交叉熵CrossEntropy ...
- C1认证学习十二(网络拓扑)
C1认证学习十二(网络拓扑) 任务背景 互联网是一个广义的概念,它泛指是一切通过网路连接在一起的计算机的集合,所以,若果只是局部观察,那就不能再说互联网是一个互联的了,那么,如果说对于一个公司来说,具 ...
- OpenCV与图像处理学习十二——图像形状特征之HOG特征
OpenCV与图像处理学习十二--图像形状特征之HOG特征 一.图像特征理解 1.1 颜色特征 1.2 纹理特征 1.3 形状特征 1.4 空间关系特征 二.形状特征描述 2.1 HOG特征 2.1. ...
- (转)SpringMVC学习(十二)——SpringMVC中的拦截器
http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...
- 【FastAPI 学习十二】定时任务篇 (移步博客园或个人网站 无广告,界面清爽整洁)
声明 目前个人放弃CSDN平台,文章只发布于个人网站和博客园 博客园地址 [FastAPI 学习十二]定时任务篇
- Js高级程序设计第三版学习(十二章)
Js高级程序设计第三版学习(十二章) 第十二章 DOM2和DOM3 1.样式: 访问样式属性 任何支持style特性的HTML元素都有一 ...
- CCIE理论-第十二篇-IPV6-NDP协议
CCIE理论-第十二篇-IPV6-NDP协议 首先我们知道 在IPV4中 A:0.0.0.1-126.255.255.255 B:128.0.0.1-191.255.255.255 C:192.0.0 ...
- BetaFlight模块设计之三十二:MSP协议模块分析
BetaFlight模块设计之三十二:MSP协议模块分析 1. MSP协议模块 1.1 MSP描述 1.2 MSP版本优缺点 1.3 MSP代码资源 2. MSP报文解析 2.1 MSP收包状态机 2 ...
最新文章
- animate默认时长所带来的问题及解决
- 【行业看点】量子通信 量子计算机 量子列车…… 量子的世界究竟多精彩?
- qt vs 不出来dos窗口_VS嵌入QT后,建立QT工程后printf和cout无效,无法产生控制台应用程序窗口,需设置工程属性...
- ElementUI中的el-table怎样实现每一列显示的是控件并能动态实现双向数据绑定
- 【深度学习】深度学习之Pytorch基础教程!
- There is no Action mapped for namespace [/]
- 分布式项目 cookie共享方案
- 第五届省赛(软件类)真题----Java大学B组答案及解析
- MySQL之逻辑架构和存储引擎
- matlab 1 3倍频分析,[转载]1/3倍频程及Matlab程序实现
- [转载] python中pprint模块详解——print()和pprint()两者的区别
- Linux 端口侦听不到,在linux上,如何在不尝试连接的情况下检查端口是否处于侦听状态...
- 16进制编辑器 linux,Tweak - Linux下的16进制编辑器
- [分享解决]你的支付授权失败。请核对你的信息并重试,或尝试其他支付方式。请联系你的银行了解更多信息
- html 文字竖排效果
- 【debug】Support for password authentication was removed on August 13, 2021.解决
- VUE项目 格林威治时间转换为北京时间
- 什么是信号完整性?(大白话)
- Pytorch 锚框
- 漏损分析与控制技术——漏损分析技术
热门文章
- UltraEdit 19.10版本花括号自动缩进两空格的解决方法
- python与js之间实现通信
- Java web Servlet弹出提示框方法
- PHPMyWind支持ppt导入
- c++使用OpenSSL基于socket实现tcp双向认证ssl(使用TSL协议)代码实现
- ARKit之路-LiDAR传感器(一)
- Atcoder Beginner Contest 260D - Draw Your Cards 解题报告
- 【ORACLE】21版本新特性之SQL宏(SQL MACROS)的分析
- 异构服务器 微服务_微服务架构是什么?
- 1.1 什么是弹性盒子?