当前位置:网站首页>Unity C # e-learning (12) -- protobuf generation protocol
Unity C # e-learning (12) -- protobuf generation protocol
2022-06-29 09:02:00 【Handsome_ shuai_】
Unity C# E-learning ( Twelve )——Protobuf Generation protocol
One . install
- Go to Protobuf Official website Download the... Of the corresponding operating system protoc, Is used to .proto File generates the protocol language file of the corresponding language

- Because I use C# So you can use the provided C# Serialization and deserialization of the project , Then compile it yourself DLL Put in Unity Use in

Two .Protobuf Configured rules (.proto The syntax of the document )
syntax = "proto3";// To determine the proto Version number of the document
package GamePlayerTest;// Namespace
import "test2.proto";
// Message class
message TestMsg1 {
// Member type Member name Unique number
// Floating point numbers
float testF = 1;
double testD = 2;
// Variable length coding
//Protobuf Will automatically optimize , You can use as few bytes as possible , To store content
int32 testInt32 = 3; // Not applicable to negative numbers
int64 testInt64 = 4;
// Better for negative numbers
sint32 testSInt32 = 5;
sint64 testSInt64 = 6;
// Unsigned variable length coding
uint32 testUInt32 = 7;
uint64 testUInt64 = 8;
// Fixed byte type
fixed32 testFixed32 = 9; // Usually used to indicate greater than 2 Of 28 The number of the power uint
fixed64 testFixed64 = 10; // Usually used to indicate greater than 2 Of 56 The number of the power ulong
sfixed32 testSFixed32 = 11; //int
sfixed64 testSFixed64 = 12; //long
// Array
repeated int32 arr_int32 = 13;
repeated string arr_string = 14;
// Dictionaries
map<int32,string> map1 = 15;
// enumeration
TestEnum1 test_enum1 = 16;
// Nested messages
message TestMsg2{
int32 test_int32 = 1;
}
TestMsg2 test_msg2 = 17;
// Nested enumeration
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;
}
3、 ... and . Generate corresponding C# Code
- open cmd window
- Get into protoc.exe In the folder ( You can also drag it directly to cmd in )
- Input conversion instruction
- protoc.exe -I= Configuration path =csharp_out= The output path Profile name
Four . Encapsulate quick generate protocol file
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();
}
}
}
5、 ... and . Serialization and deserialization of protocols
1. Text stream
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. Memory flow
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]);
}
6、 ... and .Protobuf Serialization and deserialization of ( Optimize the calling method )
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]);
}
边栏推荐
- Chengtong network disk imitation blue playing network disk source code with video tutorial
- 2022年7月系统集成项目管理工程师认证招生简章
- 闭关修炼(二十)如何做好单元测试
- Memoirs of actual combat: breaking the border from webshell
- 15 things to learn in a year of internship in famous enterprises, so you can avoid detours.
- The final of the sixth season of 2022 perfect children's model Hefei division came to a successful conclusion
- CDGA|交通行业做好数字化转型的核心是什么?
- 背包九讲——全篇详细理解与代码实现
- Let's make a summary
- Compare homekit, MI family, and zhiting family cloud edition for what scene based experiences
猜你喜欢

今年的网络安全“体检”你做了吗?

闭关修炼(二十五)基础web安全

July 2022 (advanced soft test) information system project manager certification enrollment Brochure

航芯开发板&调试器

Carbon emission reduction of second-hand trading platform, with assessment standards

Leetcode (142) - circular linked list II

Open3D 最远点采样(FPS)

2022第六季完美童模 合肥賽區 决賽圓滿落幕

2022第六季完美童模 清远赛区 海选赛圆满落幕

2022春夏系列 KOREANO ESSENTIAL重塑时装生命力
随机推荐
MQTT第二话 -- emqx高可用集群实现
【无标题】
Leetcode(142)——环形链表 II
Wallpaper applet source code double ended wechat Tiktok applet
uniapp引入组件不生效解决方法
How to recite words in tables
July 2022 (advanced soft test) information system project manager certification enrollment Brochure
Operating system product key viewing method
关于父母离婚后子女姓名变更有关问题的批复
分布式数字身份的几个“非技术”思考
Open3D 最远点采样(FPS)
Memoirs of actual combat: breaking the border from webshell
[most complete] download and installation of various versions of PS and tutorial of small test ox knife (Photoshop CS3 ~ ~ Photoshop 2022)
打印服务IP设置方案
sql server 用 administrator 权限运行吗?还是以普通用户运行呢?
航芯开发板&调试器
Differences between x86 and x64
Verilog 数据类型
Baodawei of the people's Chain: break down barriers and establish a global data governance sharing and application platform
TypeScript 变量声明 —— 类型断言