Unity启动外部程序(Process)

来源:互联网 发布:jsp与javascript的区别 编辑:程序博客网 时间:2024/06/06 17:28

启动外部程序时:直接使用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);            }        }    }

原创粉丝点击