Unity3D配置Protobuff文件

Protobuff协议简介以及样例

Protobuff是一种网络协议,可以很有效率的进行数据传输,在开发游戏中,经常会用到这个协议,我们通常定义协议是在一个.proto文件中定义数据类型,然后将.proto文件转换为.cs文件(也可以转化为其他语言,这里以cs为例),例如有如下一个.proto文件

 // [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]

这个.proto文件中定义了一些数据类型,采用的是proto3格式,但是我们在脚本中不能直接用.proto文件,在Unity开发中,需要转换为.cs文件,下面有两种方式可以实现

  • 通过这个在线网站进行转换,然后将转换的文件复制到对应的cs文件中
  • 通过protogen.exe进行转换,对应的protogen可以通过下文的百度云盘获取,也可以在github上下载对应的原始工程,本地编译,然后通过批处理命令就可以再本地进行转换,转换的主要指令是:
    protogen --csharp_out=.\ Proto.proto,其中Proto.proto是当前目录中的文件

通过不断的实践,第二种是效率较高的方式,就是配置麻烦一点,接下来我会介绍主要的步骤

批处理文件

批处理文件可以帮助我们执行一些自动化工作,比如利用protogen.exe将.proto文件转换为.cs文件,将转换好的cs文件复制到需要的目录,下面是我本地的批处理文件:

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

这个批处理文件复制到一个txt文本,然后把后缀改为.bat就行

在Unity编辑器的菜单栏中添加按钮,实现点击就能进行转换

unity可以进行编辑器扩展,可以很方便的让我们执行一些快捷操作,比如上述的bat文件,我们就可以实现,在编辑器中点击一个按钮,然后文件就自动转换了,具体的做法是在Unity工程的Asset文件中,新建一个Editor文件夹,然后在这个文件下面新建脚本,按照正确的写法就可以实现在菜单栏上新加自己的功能按钮,例如下图:
在这里插入图片描述
这里具体的cs代码如下所示:

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();
    }
}

这样就能够实现点击一次Unity菜单栏的按钮实现.proto到.cs的转换了,也不需要手动的去复制啥文件,很方便!

protogen工具下载地址

protogen工具百度网盘链接:编译好的protogen百度网盘下载,里面有示例的bat文件
提取码:1fq3

猜你喜欢

转载自blog.csdn.net/qq_41841073/article/details/127254319