Unity calls protoc.exe to generate C# script

public void GenProtoCSFile()
    {
        string protogen = UnityEngine.Application.dataPath + "/SFrame/Editor/TableEditor/ExcelTool/Editor/ExcelProto/protoc.exe";
        string protoDir = ToProtoFile.Instance.ProtoDir;
        List<string> cmds = new List<string>();
        if (!Directory.Exists(protoDir))
        {
            UnityEngine.Debug.LogError("不存在Proto文件夹");
            return;
        }
        DirectoryInfo folder = new DirectoryInfo(protoDir); // Proto所在路径
        FileInfo[] files = folder.GetFiles("*.proto");
        string outDir = "Assets/Scripts/Dependencies/Editor/ProtoScript";
        if (!Directory.Exists(outDir)) // CSharp 输出路径
        {
            Directory.CreateDirectory(outDir);
        }
        foreach (FileInfo file in files)
        {
            string cmd = protogen + " --csharp_out=" + outDir + " -I " + protoDir + " " + file.FullName;
            cmds.Add(cmd);
        }
        Cmd(cmds);
        AssetDatabase.Refresh();
    }
    public void Cmd(List<string> cmds)
    {
        Process process = new Process();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.WorkingDirectory = ".";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardError = true;
        process.OutputDataReceived += OutputHandler;
        process.ErrorDataReceived += ErrorDataHandler;
        process.Start();
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();
        for (int i = 0; i < cmds.Count; i++)
        {
            process.StandardInput.WriteLine(cmds[i]);
        }
        process.StandardInput.WriteLine("exit");
        process.WaitForExit();
    }
    private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        if (!string.IsNullOrEmpty(outLine.Data))
        {
            UnityEngine.Debug.Log(outLine.Data);
        }
    }
    private void ErrorDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        if (!string.IsNullOrEmpty(outLine.Data))
        {
            UnityEngine.Debug.LogError(outLine.Data);
        }
    }

Guess you like

Origin blog.csdn.net/LM514104/article/details/124746319