正在优化unity打包工程。想用c#
代码调用shell脚本
来实现打包xcode
导出ipa
.由于对C#代码不是很熟悉。调用shell脚本的时候遇到一些问题。现在把调用shell脚本
的C#
代码在此记录下。
使用terrminal
调用shell
脚本,可以使用终端直观看到执行结果。
string command = "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal";
string shell = ProjectPath +"/xcodebuild.sh";
string argss = shell;
System.Diagnostics.Process.Start(command,argss);
UnityEngine.Debug.Log(argss);
但是发现这个方法我暂时无法给shell
命令传递参数进去。不知道有没有人知道怎么办。
不使用terrminal
的话。倒是可以。
//用于做测试 shell传递参数需要用下面方法
string shell = ProjectPath +"/shell.sh";
string arg1 = "unity";
string arg2 = ProjectPath +"/test.log";
string argss = shell +" "+ arg1 +" " + arg2;
System.Diagnostics.Process.Start("/bin/bash", argss);
//shell.sh里面内容很简单。#!/bin/bash echo $1 >> $2
就是把第一个命令参数的值拷贝到第二个参数/test.log文件里
还可以类似这样写:
string shell = "shell.sh";
Process process = new Process ();
process.StartInfo.CreateNoWindow = false;
process.StartInfo.ErrorDialog = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = shell;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.WorkingDirectory = ProjectPath;
process.Start();
UnityEngine.Debug.Log( "process start rojectPath = !!" +ProjectPath);
string output = process.StandardOutput.ReadToEnd();
File.AppendAllText (ProjectPath + "/log.text", output); //将得到的信息写入文件中。
process.WaitForExit();
process.Close();
其中注意一点就是如果我们需要指定shell
命令当前运行的目录,
可以设置StartInfo.WorkingDirectory
属性值。
网友评论