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窗口
懶散幾年了,今天重拾學習計劃。
Unity中調(diào)用bat文件的方法和因此cmd窗口的設置:
需要添加庫
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,通過以下方式為進程進行設置:
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
關閉開啟的程序代碼:
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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
淺談C#各種數(shù)組直接的數(shù)據(jù)復制/轉(zhuǎn)換
下面小編就為大家?guī)硪黄獪\談C#各種數(shù)組直接的數(shù)據(jù)復制/轉(zhuǎn)換。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法
這篇文章主要介紹了C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法,涉及C#針對sql server數(shù)據(jù)庫的簡單查詢技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
帶著問題讀CLR via C#(筆記一)CLR的執(zhí)行模型
CLR (Common Language Runtime) 是一個可以由多種編程語言使用的“運行時”。2013-04-04
Unity中的PostProcessBuild實用案例深入解析
這篇文章主要為大家介紹了Unity中的PostProcessBuild實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05

