unityprotobuf自动生成C#

Release Protocol Buffers v3.19.4 · protocolbuffers/protobuf · GitHub

导入Source code 里面的 csharp/src/Google.Protobuf 进入Unity

拷贝其他版本的 System.Runtime.CompilerServices.Unsafe进入工程

使用protoc-3.19.4-win32 里面的exe去编译proto文件为C#

cs 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using UnityEditor;
using UnityEngine;


//F:
//cd F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto
//protoc.exe --csharp_out=./ --proto_path =./ Common.proto
//PAUSE

public static class ProtobufToCSharp
{
    //F:
    //cd F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto
    //protoc.exe --csharp_out=./ --proto_path=./ Common.proto 
    //protoc.exe --csharp_out=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto --proto_path=./ Common.proto
    //protoc.exe -I=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto --csharp_out=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto Common.proto
    //protoc.exe -I=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto --csharp_out=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto Common.proto

    private static string ProtocPathExe = Application.dataPath.Replace("client/Assets", "Common/NetProto/GenCSharp/Proto/protoc.exe");
    private static string ProtoPath = Application.dataPath.Replace("client/Assets", "Common/NetProto/Source");
    private static string OutPath = Application.dataPath + "/HotUpdate/Game/ProtobufCSharp";
    [MenuItem("Protobuf/GenerateCSharp")]
    private static void GenerateCSharp()
    {
        UnityEngine.Debug.Log(ProtocPathExe);
        UnityEngine.Debug.Log(ProtoPath);
        UnityEngine.Debug.Log(OutPath);
        DirectoryInfo directoryInfo = new DirectoryInfo(ProtoPath);
        FileInfo[] fileInfos = directoryInfo.GetFiles();
        Process cmd = new Process();
        String proArgs = "";
        foreach (var fileInfo in fileInfos)
        {
            if (fileInfo.Extension != ".proto")
                continue;
            UnityEngine.Debug.Log(fileInfo.FullName);

            proArgs += $"{fileInfo.Name} ";
        }
        cmd.StartInfo.FileName = ProtocPathExe;
        Debug.Log
        cmd.StartInfo.Arguments = $"--proto_path={ProtoPath}/ --csharp_out={OutPath} {proArgs}";
        cmd.StartInfo.UseShellExecute = false;
        cmd.Start();
        AssetDatabase.Refresh();
    }
}
相关推荐
是阿根8 小时前
unity使用iTextSharp生成PDF文件
unity·c#·游戏引擎
常城16 小时前
Unity中通过TMP使用图片字体
unity·游戏引擎
惊鸿醉16 小时前
⭐Unity 开发 | 如何通过 NTP 网络时间实现精准的跨平台时间同步【附完整源码 + UI 模块 + 偏差分析】
网络·unity·游戏引擎
海鸥-w1 天前
Unity中使用Cinemachine插件创建自由视角相机(freelookCamera)来实现第三人称漫游
数码相机·unity·游戏引擎
EQ-雪梨蛋花汤1 天前
【Unity笔记】Unity音效管理:ScriptableObject配置 + 音量控制 + 编辑器预览播放自动化实现
笔记·unity·编辑器
qq_431331352 天前
Unity ML-Agents + VScode 环境搭建 Windows
windows·vscode·unity·强化学习
林枫依依2 天前
Unity 创建、读取、改写Excel表格数据
unity·excel
EQ-雪梨蛋花汤2 天前
【Unity笔记】Unity 编辑器扩展:一键查找场景中组件引用关系(含完整源码)(组件引用查找工具实现笔记)
笔记·unity·编辑器
枫景Maple2 天前
Unity常用内置变换矩阵
unity·游戏引擎
太妃糖耶2 天前
URP-UGUI交互功能实现
unity·游戏引擎·shader