当前位置:网站首页>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]);
}
边栏推荐
猜你喜欢

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

io流的总结

Scenario analysis of deadlock during MySQL insert

ThreadLocal thread variable

MT-yolov6训练及测试

批量处理实验接触角数据-MATLAB分析

ActiveMQ message component publish subscribe redelivery message redelivery

TypeScript 變量聲明 —— 類型斷言
![[most complete] download and installation of various versions of PS and tutorial of small test ox knife (Photoshop CS3 ~ ~ Photoshop 2022)](/img/6d/4d8d90dd221de697f4c2ab5dcc7f96.png)
[most complete] download and installation of various versions of PS and tutorial of small test ox knife (Photoshop CS3 ~ ~ Photoshop 2022)
Working for many years, recalling life -- three years in high school
随机推荐
TypeScript 變量聲明 —— 類型斷言
Development tips - Image Resource Management
Verilog 表达式
Wallpaper applet source code double ended wechat Tiktok applet
Let's make a summary
MQTT第二话 -- emqx高可用集群实现
手写 redux-thunk
ThreadLocal thread variable
51 MCU interrupt and timer counter, based on Puzhong technology hc6800-esv2.0
2022 Season 6 perfect children's model Haikou competition area trial successfully concluded
How to recite words in tables
Backpack Lecture 9 - detailed understanding and code implementation
Transformer details
cokkie和session的区别
Pointnet的网络学习
verilog 归约操作符
Dialogue | prospects and challenges of privacy computing in the digital age
mysql insert 时出现Deadlock死锁场景分析
First electric shock, so you are such a dragon lizard community | dragon lizard developer said that issue 8
Let you know today that the passing rate of the PMP Exam is 97%, is it believable