如何在unity中開啟內部exe並迴圈切換
阿新 • • 發佈:2019-01-27
近日接到一個新任務,大概意思是把好多exe檔案組合到一起,然後可以隨意在此場景進入某個exe程式,退出的時候再回到這個場景,說實話這個要求真是策劃突發奇想!怎莫會有這種要求?一開始為神魔不做到一個工程下?原因很簡單,太大了。。。。。不過,有幸來這裡的同志們我已經給你們鋪好路了!!!!!(我的主程式是<開始.exe>,化工.exe與中醫.exe都在StreamingAssets目錄下)
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Diagnostics; public class TotalManager : MonoBehaviour { //課程字串集合 List<string> _process = new List<string>(); public GameObject totalProcess; //獲取htc手柄按鍵 SteamVR_Controller.Device devicedemo; //獲取htc手柄 SteamVR_TrackedObject trackobject; //定義一個載入路徑(化工與中醫的) private string path; public bool stop; // Use this for initialization void Start () { trackobject = transform.GetComponent<SteamVR_TrackedObject>(); //便歷壓到集合中來 for (int i = 0; i < totalProcess.transform.childCount; i++) { _process.Add(totalProcess.transform.GetChild(i).name); } } // Update is called once per frame void Update () { devicedemo = SteamVR_Controller.Input((int)trackobject.index); } private void OnTriggerStay(Collider other) { if (other.CompareTag("touch")) { //當按下圓盤鍵 跟要選的課相碰 if (devicedemo.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)) { if (_process.Contains(other.transform.name)) { print("aaaa"); //所有計算機內活著的程序(每一個exe都是一個程序) Process[] processes = Process.GetProcesses(); foreach (Process process in processes) { try { //只執行一遍(不加bool 有可能卡死) if (stop ==false) { // 開啟streamingAssets下的exe Process.Start(Application.streamingAssetsPath+"/"+ other.gameObject.name); stop = true; } if (!process.HasExited) { //關閉主程式(貌似可以不管。。) if (process.ProcessName == "開始") { process.Kill(); UnityEngine.Debug.Log("已殺死程序"); } } } catch (System.InvalidOperationException ex) { UnityEngine.Debug.Log(ex); } } } } } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Diagnostics; using System; using UnityEngine.UI; public class Back_huangong : MonoBehaviour { public Text obj; SteamVR_Controller.Device devicedemo; SteamVR_TrackedObject trackobject; public bool stop; private string totalpath; private string currentpath; // Use this for initialization void Start() { trackobject = transform.GetComponent<SteamVR_TrackedObject>(); } // Update is called once per frame void Update() { devicedemo = SteamVR_Controller.Input((int)trackobject.index); } private void OnTriggerStay(Collider other) { if (other.CompareTag("touch")) { if (devicedemo.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)) { //streamAssets總路徑 totalpath = Application.streamingAssetsPath; string[] strArr = totalpath.Split(new char[] { '開' }); //開始路徑也就是主程式(這個最重要) currentpath = strArr[0] + "開始"; if (other.gameObject.name == "開始") { obj.text = "化工回城"; Process[] processes = Process.GetProcesses(); foreach (Process process in processes) { try { if (stop == false) { Process.Start(currentpath); stop = true; obj.text = "化工回城lelel"; } if (!process.HasExited) { if (process.ProcessName == "化工") { process.Kill(); UnityEngine.Debug.Log("已殺死程序"); } } } catch (System.InvalidOperationException ex) { UnityEngine.Debug.Log(ex); } } } } } } }