有时候我们想把一些外部命令集成到unity中,比如点击unity的一个按钮就可以打开导出的项目,或者执行一些项目更新等。
很简单,直接上代码:
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Threading;
using System.IO;
using UnityEditor;
public class CommandForUnity
{
public static void LaunchAppllication ()
{
#if UNITY_EDITOR_OSX
// windows上面的.exe相对路径
string path = Application.dataPath + "/../Relase/Yummy.app";
UnityEngine.Debug.Log("path:" + path);
processCommand ("open", path);
#else
// windows上面的.exe相对路径
string path = Application.dataPath + "/../Relase/Yummy.exe";
Application.OpenURL(path);
#endif
}
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);
p.WaitForExit ();
p.Close ();
}
}
#endif
以上实现的功能是处于编辑器模式下,绘制一个按钮,比如“试玩”按钮,点击后直接打开我们的游戏,很简单的吧。拿去直接用吧,更改一下路径就可以了。