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#
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
//PAUSEpublic 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.protoprivate 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.Logcmd.StartInfo.Arguments = $"--proto_path={ProtoPath}/ --csharp_out={OutPath} {proArgs}";cmd.StartInfo.UseShellExecute = false;cmd.Start();AssetDatabase.Refresh();}
}