[实用工具]Unity调用外部EXE或Shell命令

版权所有,转载须注明出处!
喜欢火影、喜欢Java、喜欢unity3D、喜欢游戏开发的都可以加入 木叶村Q群:379076227

1、开门见山的需求
有的时候,我们想把一些外部命令集成到 unity 中,比如,你想通过点击Unity中的一个按钮,就更新SVN(假设该项目是受SVN管理的)。
那么,就涉及到一个Unity调用外部可执行文件、bat/shell等。
这个需求是挺常见的,也是不难实现的。

2、简单明了的实现
我们先封装一个命令调用的函数:
[C#]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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();
}


好了,关于上述代码相关的,如果大家有兴趣可以自行去msdn查API。
不关心的,继续往下看怎么使用。

3、细致耐心的命令行教学
上面封装的函数,第一个参数即命令名,第二个参数为这个命令接受的参数。
对于没有接触命令行的同学。可以现在跟我做一遍感受下命令行。如果对命令行比较熟悉的同学,可以直接跳到第4步。
以下操作在WIN7中。
A:按下win+R,在出现的运行窗口输入“cmd”,然后回车。
B:这时候会出现一个命令行窗口。
C:输入命令:notepad,然后回车
 
D:会发现打开了记事本。
 

E:假设你D盘有个文件1.txt
F:那么你可以在命令行输入:notepad D:\1.txt 来直接使用记事本打开D盘的1.txt。
 

简单来说,上面的notepad就是我们要传入的封装好的函数的第一个参数。而D:\1.txt就是我们的第二个参数。


那么可执行的命令有哪些呢?
在系统变量PATH中所有路径下的exe、bat/bash(linux) 文件都可以执行。
你可以通过在命令行中输入:echo %PATH%  来获得PATH的信息。
 

也可以在“桌面”--“计算机”--“右键”--“属性”--“高级系统设置”,在“环境变量”中查看PATH
 

 

双击这个path可以查看和编辑。


如果没有在path中的路径。你想调用路径下的exe只能用全路径了。比如你想调用  D:\AA\BB\CC\d.exe 这个exe。PATH中没有D:\AA\BB\CC的话。那么,你在命令行每次都要输入 D:\AA\BB\CC\d.exe来调用他。如果PATH中有D:\AA\BB\CC。那么你在命令行可以直接输入d.exe来调用他。
好了。关于命令行的就讲这么多啦~~进入第4步。


4、凶残粗暴地调用第二步定义的函数
比如我们现在想更新SVN,只要在Unity中找个地方(比如你写个)执行下面的代码:
[C#]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
         }
 
}


然后就发现Unity中多了SVN/Update的按钮。你点击他的时候他就更新SVN了咯。
 
  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值