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();
    }
}
相关推荐
Artistation Game14 小时前
九、怪物行为逻辑
游戏·unity·游戏引擎
百里香酚兰14 小时前
【AI学习笔记】基于Unity+DeepSeek开发的一些BUG记录&解决方案
人工智能·学习·unity·大模型·deepseek
dangoxiba17 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
先生沉默先1 天前
使用Materialize制作unity的贴图,Materialize的简单教程,Materialize学习日志
学习·unity·贴图
十画_8242 天前
Visual Studio 小技巧记录
unity·visual studio
red_redemption2 天前
cpp,git,unity学习
git·unity·游戏引擎
tealcwu2 天前
【Unity踩坑】Unity更新Google Play结算库
unity·游戏引擎
先生沉默先2 天前
unity 默认渲染管线材质球的材质通道,材质球的材质通道
unity·游戏引擎·材质
白鹭float.2 天前
【Unity AI】基于 WebSocket 和 讯飞星火大模型
人工智能·websocket·unity
一个程序员(●—●)2 天前
Unity各个操作功能+基本游戏物体创建与编辑+Unity场景概念及文件导入导出
unity·游戏引擎