Unity3D configuration Protobuff file

Introduction and samples of Protobuff protocol

Protobuff is a network protocol that can transmit data efficiently. This protocol is often used in the development of games. We usually define the protocol by defining the data type in a .proto file, and then convert the .proto file to .cs file (can also be converted into other languages, cs is used as an example here), for example, there is the following .proto file

 // [START declaration]
syntax = "proto3";
package Proto;
// [END declaration]

// [START messages]

message PlaceHolder
{
    
    
string dec=1;
string account=2;
bool flag=3;
}

message Register
{
    
    
    string	account=2;
    string	password=3;
    int32 funID = 1;
} 

message RegisterRes
{
    
    
    int32 money=1;
    bool	isScuess =2;
    string	tips =3;
    int32 funID = 4;
    string	name=5;
} 


message Login
{
    
    
    string	password=1;
    string	account=2;
} 

message LoginRes
{
    
    
    bool	isScuess =1;
    string	tips =2;
    int32 funID = 3;
    string	name=4;
    int32 money=5;
    string	account=6;
    string	password=7;
} 

message MatchJoin
{
    
    
string joinName=1;
string joinAccount=2;
bool is_join=3;
int32 Seatidx=4;
}

message MatchState
{
    
    

string dec=1;
int32 matchState=2;
}

message TalkInfo
{
    
    

    string	fromNmae =1;
    string	toName =2;
    int32 funID = 3;
} 

message AvatarInfo
{
    
    
string name=1;
string account=2;
int32 money=3;
int32 team_id = 4;
repeated int32 cards=5;
}

message MatchingSuccess
{
    
    
map<int32, AvatarInfo> AvatarInfos = 1;

} 

message PlayedCardInfo
{
    
    
int32 team_id=1;
string account=2;
int32 card_value=3;
int32 out_card_value=4;
int32 cards_left_num=5;
map<int32, AvatarInfo> AvatarInfos = 6;
} 

message SyncPlayerInfo
{
    
    
bool can_play=1;
int32 money=2;
string name=3;
int32 match_state=4;
}

// [END messages]

Some data types are defined in this .proto file, which uses the proto3 format, but we cannot directly use the .proto file in the script. In Unity development, it needs to be converted into a .cs file. There are two ways to achieve it

  • Convert through this online website, and then copy the converted file into the corresponding cs file
  • Convert through protogen.exe, the corresponding protogen can be obtained through the Baidu cloud disk below, or you can download the corresponding original project on github, compile it locally, and then convert it locally through a batch command. The main command for conversion is :
    protogen --csharp_out=.\Proto.proto, where Proto.proto is a file in the current directory

Through continuous practice, the second is a more efficient way, which is a little more troublesome to configure. Next, I will introduce the main steps

batch file

Batch files can help us perform some automation tasks, such as using protogen.exe to convert .proto files to .cs files, and copy the converted cs files to the required directory. The following is my local batch file:

rem 将对应目录下的.proto文件复制到当前文件,因为当前位置有protogen.exe及其dll环境
xcopy /y E:\unity-2020\2020.3.8f1c1\Editor\project_0\NewUnityProject\Assets\Scripts\proto\Proto.proto .\ /e
rem 执行对应的proto 到 .cs的转换
protogen --csharp_out=.\ Proto.proto

rem client脚本所在位置
set client_pos="E:\unity-2020\2020.3.8f1c1\Editor\project_0\NewUnityProject\Assets\Scripts\proto\"
rem server脚本所在位置
set server_pos=E:\unity-2020\2020.3.8f1c1\Editor\project_0\C_S_Prac\Server_Prac\Server_Prac\Proro\

rem 将Proto.cs辅助到client脚本所在位置
xcopy /y Proto.cs %client_pos% /e

rem 将Proto.proto辅助到client脚本所在位置
xcopy /y Proto.proto %client_pos% /e

rem 将Proto.cs辅助到server脚本所在位置
xcopy /y Proto.cs %server_pos% /e

rem 将Proto.proto辅助到server脚本所在位置
xcopy /y Proto.proto %server_pos% /e
pause

Copy this batch file to a txt text, and then change the suffix to .bat

Add a button to the menu bar of the Unity editor to realize conversion by clicking

Unity can expand the editor, which can be very convenient for us to perform some shortcut operations, such as the bat file mentioned above, we can achieve it, click a button in the editor, and then the file will be automatically converted, the specific method is in Unity In the Asset file of the project, create a new Editor folder, and then create a new script under this file. According to the correct writing method, you can add your own function button on the menu bar, for example, as shown in the figure below: the specific cs code here is as
insert image description here
follows :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Diagnostics;
using System.IO;

public class DoProto2CS : EditorWindow
{
    
    
    // Start is called before the first frame update
    [MenuItem("Proto/RefreshProro2CS")]
    private static void ShowWindow()
    {
    
    
    //这个函数主要作用是执行编写好的一个批处理文件

        string batPath = "E:/unity-2020/2020.3.8f1c1/Editor/project_0/Proto2CSFile/build_proto.bat";//批处理文件地址
        Process pro = new Process();


        FileInfo file = new FileInfo(batPath);
        pro.StartInfo.WorkingDirectory = file.Directory.FullName;
        pro.StartInfo.FileName = batPath;
        pro.StartInfo.CreateNoWindow = false;
        pro.Start();
        pro.WaitForExit();
    }
}

In this way, it is possible to click the button on the Unity menu bar once to realize the conversion from .proto to .cs, and there is no need to manually copy any files, which is very convenient!

Protogen tool download address

protogen tool Baidu network disk link: compiled protogen Baidu network disk download, there is a sample bat file
extraction code: 1fq3

Guess you like

Origin blog.csdn.net/qq_41841073/article/details/127254319