提交 a4dc1496 编写于 作者: 魔术师Dix's avatar 魔术师Dix

【Utils】增加编辑器下运行Bat文件的方法;

上级 2fb240d6
......@@ -8,6 +8,7 @@
*/
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
......@@ -754,6 +755,42 @@ namespace Aster.AsterBinary
o.CopyTo(dir, fileName);
}
public static void RunBat(string filePath, bool showWindow = false)
{
if (!File.Exists(filePath))
{
Logger.LogError($"目标路径不存在:{filePath}");
return;
}
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = filePath; // .bat文件的完整路径
startInfo.Arguments = ""; // 如果.bat文件需要参数,可以在这里添加
startInfo.UseShellExecute = false; // 必须为false以便能够重定向I/O流
startInfo.CreateNoWindow = !showWindow; // 是否显示命令提示符窗口(可选)
// 如果希望看到.bat文件的输出
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
startInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;
startInfo.WindowStyle = ProcessWindowStyle.Normal; // 或Hidden来隐藏窗口
using (Process process = new Process())
{
process.StartInfo = startInfo;
process.OutputDataReceived += (sender, args) => Logger.Log(args.Data);
process.ErrorDataReceived += (sender, args) => Logger.LogError(args.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// 如果需要等待.bat文件执行完成再继续
process.WaitForExit();
}
}
#endregion
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册