前提是python.exe要用輸入進你的環境參數
在安裝python那篇有教學過
要檢查有沒有成功調整環境參數,就使用CMD 來查詢 python就可以囉
以下就可以成功的調用了

using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using UnityEngine; public class Record : MonoBehaviour { System.Diagnostics.Process process = new System.Diagnostics.Process (); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo (); Process proc = new Process (); Boolean run = false; // Use this for initialization void Start () { if (run == false) { string progToRun = @"E:\screenshot.py"; //調用py的位置 proc.StartInfo.FileName = @"python.exe"; //執行的程式 proc.StartInfo.UseShellExecute = false; //是否使用操作系统shell啟動 proc.StartInfo.RedirectStandardInput = true; //接受來自調用程序的輸入訊息 proc.StartInfo.RedirectStandardOutput = true; //由調用程序獲取輸出訊息 proc.StartInfo.RedirectStandardError = true; //重定向標準錯誤輸出 proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal; //不創建窗口 proc.StartInfo.CreateNoWindow = true; proc.StartInfo.Arguments = string.Concat (progToRun); proc.Start (); run = true; } } // Update is called once per frame void Update () { if (Input.GetKeyDown (KeyCode.A)) { //按A強迫停止 string ProcessName = "python"; //換成想要結束的進程名字 Process[] MyProcess = Process.GetProcessesByName (ProcessName); MyProcess[0].Kill (); UnityEngine.Debug.Log ("close"); } } }
@copyright MRcodingRoom
觀看更多文章請點MRcoding筆記
觀看更多文章請點MRcoding筆記