// C# example. using UnityEditor; using UnityEngine; using System.Runtime.InteropServices; using System.IO; using System.Diagnostics; using System; public class ScriptBatch { [MenuItem("Protobuf/Build C# GRPC connecting code")] public static void BuildGRPC() { string arch = null; if (RuntimeInformation.ProcessArchitecture == Architecture.Arm || RuntimeInformation.ProcessArchitecture == Architecture.X86) { arch = "x86"; } else { arch = "x64"; } string protoc = null; string plugin = null; string basePath = Application.dataPath.Substring(0, Application.dataPath.Length - "/Assets".Length); string protobufPath = Path.Combine(Application.dataPath.Substring(0, Application.dataPath.Length - "/unity/Assets".Length), "protobuf"); string outDir = Path.Combine(Application.dataPath, "Scripts", "grpc"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { protoc = basePath + "/Packages/Grpc.Tools.2.33.1/tools/linux_" + arch + "/protoc"; plugin = basePath + "/Packages/Grpc.Tools.2.33.1/tools/linux_" + arch + "/grpc_csharp_plugin"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { protoc = basePath + "\\Packages\\Grpc.Tools.2.33.1\\tools\\windows_" + arch + "\\protoc.exe"; plugin = basePath + "\\Packages\\Grpc.Tools.2.33.1\\tools\\windows_" + arch + "\\grpc_csharp_plugin.exe"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { protoc = basePath + "/Packages/Grpc.Tools.2.33.1/tools/macosx_" + arch + "/protoc"; plugin = basePath + "/Packages/Grpc.Tools.2.33.1/tools/macosx_" + arch + "/grpc_csharp_plugin"; } UnityEngine.Debug.Log(protoc); UnityEngine.Debug.Log(plugin); // // For the example // const string ex1 = "C:\\"; // const string ex2 = "C:\\Dir"; // // Use ProcessStartInfo class try{ var protoFiles = Directory.EnumerateFiles(protobufPath, "*.proto"); foreach (string currentFile in protoFiles) { compile_file(protoc, plugin, protobufPath, outDir, currentFile); } } catch (Exception e) { UnityEngine.Debug.LogException(e); } // try // { // // Start the process with the info we specified. // // Call WaitForExit and then the using statement will close. // using (Process exeProcess = Process.Start(startInfo)) // { // exeProcess.WaitForExit(); // } // } // catch // { // // Log error. // } } public static void compile_file(string protoc, string plugin, string protobufPath, string outDir, string file) { // -I protobuf --csharp_out=unity/Assets/Scripts/grpc --grpc_out=unity/Assets/Scripts/grpc --plugin=protoc-gen-grpc=unity/Packages/Grpc.Tools.2.33.1/tools/linux_x64/grpc_csharp_plugin file Process myProcess = new Process(); myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; myProcess.StartInfo.CreateNoWindow = true; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.FileName = protoc; // string path = "C:\\Users\\Brian\\Desktop\\testFile.bat"; myProcess.StartInfo.Arguments = $"-I \"{protobufPath}\" --csharp_out=\"{outDir}\" --grpc_out=\"{outDir}\" --plugin=protoc-gen-grpc=\"{plugin}\" \"{file}\""; myProcess.EnableRaisingEvents = true; myProcess.StartInfo.RedirectStandardError = true; myProcess.Start(); myProcess.WaitForExit(); if (myProcess.ExitCode != 0) { throw new IOException($"Protoc error: {myProcess.StandardError.ReadToEnd()}"); }else{ UnityEngine.Debug.Log($"Compiled {file}"); } } }