Unity C#執(zhí)行bat腳本的操作
我們先封裝一下接口,如下,把EdtUtil.cs放置在Assets/Editor目錄中
// EdtUtil.cs using System; using UnityEditor; using UnityEngine; using System.Collections.Generic; using System.IO; using System.Threading; using System.Text; class EdtUtil { public static System.Diagnostics.Process CreateShellExProcess(string cmd, string args, string workingDir = "") { var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd); pStartInfo.Arguments = args; pStartInfo.CreateNoWindow = false; pStartInfo.UseShellExecute = true; pStartInfo.RedirectStandardError = false; pStartInfo.RedirectStandardInput = false; pStartInfo.RedirectStandardOutput = false; if (!string.IsNullOrEmpty(workingDir)) pStartInfo.WorkingDirectory = workingDir; return System.Diagnostics.Process.Start(pStartInfo); } public static void RunBat(string batfile, string args, string workingDir = "") { var p = CreateShellExProcess(batfile, args, workingDir); p.Close(); } public static string FormatPath(string path) { path = path.Replace("/", "\\"); if (Application.platform == RuntimePlatform.OSXEditor) path = path.Replace("\\", "/"); } }
現(xiàn)在,我們在工程Assets外層有一個batFiles目錄,里面有一個gen_client_cfg.bat腳本
我們想通過Unity菜單執(zhí)行這個腳本,例
using UnityEngine; using UnityEditor; class Test { private static void RunMyBat(string batFile,string workingDir) { var path = EdtUtil.FormatPath(workingDir); if (!System.IO.File.Exists(path)) { GameLogger.LogError("bat文件不存在:" + path); } else { EdtUtil.RunBat(batFile, "", path); } } [MenuItem("Tools/執(zhí)行g(shù)en_client_cfg.bat")] private static void Run() { // 執(zhí)行bat腳本 RunBat("gen_client_cfg.bat", Application.dataPath + "/../batFiles/"); } }
點擊菜單 【Tools】-【執(zhí)行g(shù)en_client_cfg.bat】即可在Unity中直接執(zhí)行bat腳本了
補充:unity運行bat文件并隱藏cmd窗口
懶散幾年了,今天重拾學(xué)習(xí)計劃。
Unity中調(diào)用bat文件的方法和因此cmd窗口的設(shè)置:
需要添加庫
using System.Diagnostics;
方法代碼:
public static System.Diagnostics.Process CreateShellExProcess(string cmd, string args, string workingDir = "") { var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd); pStartInfo.Arguments = args; pStartInfo.CreateNoWindow = false; pStartInfo.UseShellExecute = true; // pStartInfo.WindowStyle = ProcessWindowStyle.Hidden; pStartInfo.RedirectStandardError = false; pStartInfo.RedirectStandardInput = false; pStartInfo.RedirectStandardOutput = false; if (!string.IsNullOrEmpty(workingDir)) pStartInfo.WorkingDirectory = workingDir; return System.Diagnostics.Process.Start(pStartInfo); } public void RunBat(string batfile, string args, string workingDir = "") { var p = CreateShellExProcess(batfile, args, workingDir); p.Close(); }
上面代碼注釋掉的那行就是隱藏窗口的方法。需要注意的是:
如果proc.StartInfo.UseShellExecute為false,使用:
proc.StartInfo.CreateNoWindow = true;
如果proc.StartInfo.UseShellExecute為true,通過以下方式為進程進行設(shè)置:
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
關(guān)閉開啟的程序代碼:
static System.Diagnostics.Process CreateShellExProcess(string cmd, string args, string workingDir = "") { var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd); pStartInfo.Arguments = args; pStartInfo.CreateNoWindow = false; pStartInfo.UseShellExecute = true; pStartInfo.WindowStyle = ProcessWindowStyle.Hidden; pStartInfo.RedirectStandardError = false; pStartInfo.RedirectStandardInput = false; pStartInfo.RedirectStandardOutput = false; if (!string.IsNullOrEmpty(workingDir)) pStartInfo.WorkingDirectory = workingDir; return System.Diagnostics.Process.Start(pStartInfo); } public void RunBat(string batfile, string args, string workingDir = "") { var p = CreateShellExProcess(batfile, args, workingDir); p.Close(); }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#使用三層架構(gòu)開發(fā)Winform的詳細案例
這篇文章介紹了C#使用三層架構(gòu)開發(fā)Winform的詳細案例,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04C#創(chuàng)建一個小型Web Server(Socket實現(xiàn))
這篇文章主要介紹了關(guān)于C#利用Socket實現(xiàn)創(chuàng)建一個小型Web Server的相關(guān)資料,文中通過示例代碼介紹的很詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02C#中析構(gòu)函數(shù)、Dispose、Close方法的區(qū)別
本文詳細對比了C#中析構(gòu)函數(shù)、Dispose和Close方法的區(qū)別,三者都是釋放資源,本文介紹了他們各自的使用方法和使用場景,希望對大家有所幫助。2016-04-04