Unity启动外部程序(Process)

启动外部程序时:直接使用Process.Start();来启动外部程序,参数(需要启动的外部程序所在文件位置)

关闭外部程序时:使用 process.Kill();来关闭外部程序

private string exePath;

void Start()
{
exePath = @"C:\Windows\System32\calc.exe";
}

void OnGUI()
{
if (GUI.Button(new Rect(100, 100, 150, 50), "Start Cale"))
{
Process.Start(exePath);
}

if (GUI.Button(new Rect(100, 200, 150, 50), "Stop Cale"))
{
KillProcess("Calculator");
}
}

void KillProcess(string processName)
{
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
try
{
if (!process.HasExited)
{
if (process.ProcessName == processName)
{
process.Kill();
UnityEngine.Debug.Log("已杀死进程");
}
}
}
catch (System.InvalidOperationException ex)
{
UnityEngine.Debug.Log(ex);
}
}
}

猜你喜欢

转载自www.cnblogs.com/lingLuoChengMi/p/9662115.html