Unity 如何调用同级目录下 bat 文件
在 Unity 中调用与 Assets 目录同级的 luban 文件夹下的 bat 文件,可以使用 System.Diagnostics 命名空间中的 Process 类来实现。下面是一个示例代码:
using UnityEngine;
using System.Diagnostics;
public class RunBatFile : MonoBehaviour
{
void Start()
{
string batPath = Application.dataPath + "/../luban/batfile.bat"; // 获取 bat 文件的路径
Process process = new Process();
process.StartInfo.FileName = "cmd.exe"; // 使用 cmd.exe 运行 bat 文件
process.StartInfo.Arguments = "/c " + batPath; // 设置执行的参数为 bat 文件的路径
process.StartInfo.CreateNoWindow = true; // 不创建新窗口
process.StartInfo.UseShellExecute = false; // 不使用操作系统外壳程序启动进程
process.StartInfo.RedirectStandardOutput = true; // 重定向输出
process.Start(); // 启动进程
string output = process.StandardOutput.ReadToEnd(); // 读取输出
UnityEngine.Debug.Log(output); // 输出到 Unity 的控制台
process.WaitForExit(); // 等待进程退出
process.Close(); // 关闭进程
}
}
在上述代码中,首先获取 bat 文件的路径,然后创建一个 Process 对象,并设置其 StartInfo 属性,包括要执行的命令、参数、窗口、输出等设置。接下来,调用 Start 方法启动进程,读取输出并输出到 Unity 的控制台,最后等待进程退出并关闭进程。
请注意,上述代码中的 bat 文件路径是相对于 Unity 项目根目录的相对路径。如果 luban 文件夹不在 Assets 同级目录下,可以根据实际情况进行调整。
原文地址: http://www.cveoy.top/t/topic/epf2 著作权归作者所有。请勿转载和采集!