1、開門見山的需求有的時候饭弓,我們想把一些外部命令集成到unity中双饥,比如,你想通過點擊Unity中的一個按鈕弟断,就更新SVN(假設(shè)該項目是受SVN管理的)咏花。那么,就涉及到一個Unity調(diào)用外部可執(zhí)行文件、bat/shell等昏翰。這個需求是挺常見的苍匆,也是不難實現(xiàn)的。2棚菊、簡單明了的實現(xiàn)我們先封裝一個命令調(diào)用的函數(shù):[C#] 純文本查看 復(fù)制代碼
private static void processCommand(string command, string argument){
ProcessStartInfo start = new ProcessStartInfo(command);
start.Arguments = argument;
start.CreateNoWindow = false;
start.ErrorDialog = true;
start.UseShellExecute = true;
if(start.UseShellExecute){
start.RedirectStandardOutput = false;
start.RedirectStandardError = false;
start.RedirectStandardInput = false;
} else{
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.RedirectStandardInput = true;
start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
}
Process p = Process.Start(start);
if(!start.UseShellExecute){
printOutPut(p.StandardOutput);
printOutPut(p.StandardError);
}
p.WaitForExit();
p.Close();
}
好了浸踩,關(guān)于上述代碼相關(guān)的,如果大家有興趣可以自行去msdn查API统求。不關(guān)心的检碗,繼續(xù)往下看怎么使用。3码邻、細致耐心的命令行教學(xué)上面封裝的函數(shù)折剃,第一個參數(shù)即命令名,第二個參數(shù)為這個命令接受的參數(shù)像屋。對于沒有接觸命令行的同學(xué)怕犁。可以現(xiàn)在跟我做一遍感受下命令行己莺。如果對命令行比較熟悉的同學(xué)奏甫,可以直接跳到第4步。以下操作在WIN7中凌受。A:按下win+R扶檐,在出現(xiàn)的運行窗口輸入“cmd”,然后回車胁艰。B:這時候會出現(xiàn)一個命令行窗口款筑。C:輸入命令:notepad,然后回車D:會發(fā)現(xiàn)打開了記事本腾么。
E:假設(shè)你D盤有個文件1.txtF:那么你可以在命令行輸入:notepad D:\1.txt 來直接使用記事本打開D盤的1.txt奈梳。
簡單來說,上面的notepad就是我們要傳入的封裝好的函數(shù)的第一個參數(shù)解虱。而D:\1.txt就是我們的第二個參數(shù)攘须。那么可執(zhí)行的命令有哪些呢?在系統(tǒng)變量PATH中所有路徑下的exe殴泰、bat/bash(linux) 文件都可以執(zhí)行于宙。你可以通過在命令行中輸入:echo %PATH% 來獲得PATH的信息。
也可以在“桌面”--“計算機”--“右鍵”--“屬性”--“高級系統(tǒng)設(shè)置”悍汛,在“環(huán)境變量”中查看PATH
雙擊這個path可以查看和編輯捞魁。如果沒有在path中的路徑。你想調(diào)用路徑下的exe只能用全路徑了离咐。比如你想調(diào)用 D:\AA\BB\CC\d.exe 這個exe谱俭。PATH中沒有D:\AA\BB\CC的話奉件。那么,你在命令行每次都要輸入 D:\AA\BB\CC\d.exe來調(diào)用他昆著。如果PATH中有D:\AA\BB\CC县貌。那么你在命令行可以直接輸入d.exe來調(diào)用他。好了凑懂。關(guān)于命令行的就講這么多啦~~進入第4步煤痕。4、兇殘粗暴地調(diào)用第二步定義的函數(shù)比如我們現(xiàn)在想更新SVN接谨,只要在Unity中找個地方(比如你寫個)執(zhí)行下面的代碼:[C#] 純文本查看 復(fù)制代碼
public class SvnForUnity{
static string SVN_BASE = "F:\\Project\\Game";
[MenuItem("SVN/Update", false, 1)]
public static void SvnUpdate(){
processCommand("svn", "update \""+SVN_BASE+"\"");
}
private static void processCommand(string command, string argument){
ProcessStartInfo start = new ProcessStartInfo(command);
start.Arguments = argument;
start.CreateNoWindow = false;
start.ErrorDialog = true;
start.UseShellExecute = true;
if(start.UseShellExecute){
start.RedirectStandardOutput = false;
start.RedirectStandardError = false;
start.RedirectStandardInput = false;
} else{
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.RedirectStandardInput = true;
start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
}
Process p = Process.Start(start);
if(!start.UseShellExecute){
printOutPut(p.StandardOutput);
printOutPut(p.StandardError);
}
p.WaitForExit();
p.Close();
}
}
然后就發(fā)現(xiàn)Unity中多了SVN/Update的按鈕摆碉。你點擊他的時候他就更新SVN了咯。