当前位置:   article > 正文

unityprotobuf自动生成C#_unity protobuf 转c#

unity protobuf 转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#

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. using UnityEditor;
  8. using UnityEngine;
  9. //F:
  10. //cd F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto
  11. //单文件生成
  12. //protoc.exe --csharp_out=./ --proto_path =./ Common.proto
  13. //整个文件夹下生成
  14. //protoc --proto_path=./src --csharp_out=../Proto ./src/*.proto
  15. //PAUSE
  16. public static class ProtobufToCSharp
  17. {
  18. //F:
  19. //cd F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto
  20. //protoc.exe --csharp_out=./ --proto_path=./ Common.proto
  21. //protoc.exe --csharp_out=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto --proto_path=./ Common.proto
  22. //protoc.exe -I=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto --csharp_out=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto Common.proto
  23. //protoc.exe -I=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto --csharp_out=F:\TUANJIEProject\client\Common\NetProto\GenCSharp\Proto Common.proto
  24. private static string ProtocPathExe = Application.dataPath.Replace("client/Assets", "Common/NetProto/GenCSharp/Proto/protoc.exe");
  25. private static string ProtoPath = Application.dataPath.Replace("client/Assets", "Common/NetProto/Source");
  26. private static string OutPath = Application.dataPath + "/HotUpdate/Game/ProtobufCSharp";
  27. [MenuItem("Protobuf/GenerateCSharp")]
  28. private static void GenerateCSharp()
  29. {
  30. UnityEngine.Debug.Log(ProtocPathExe);
  31. UnityEngine.Debug.Log(ProtoPath);
  32. UnityEngine.Debug.Log(OutPath);
  33. DirectoryInfo directoryInfo = new DirectoryInfo(ProtoPath);
  34. FileInfo[] fileInfos = directoryInfo.GetFiles();
  35. Process cmd = new Process();
  36. String proArgs = "";
  37. foreach (var fileInfo in fileInfos)
  38. {
  39. if (fileInfo.Extension != ".proto")
  40. continue;
  41. UnityEngine.Debug.Log(fileInfo.FullName);
  42. proArgs += $"{fileInfo.Name} ";
  43. }
  44. cmd.StartInfo.FileName = ProtocPathExe;
  45. Debug.Log
  46. cmd.StartInfo.Arguments = $"--proto_path={ProtoPath}/ --csharp_out={OutPath} {proArgs}";
  47. cmd.StartInfo.UseShellExecute = false;
  48. cmd.Start();
  49. AssetDatabase.Refresh();
  50. }
  51. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号