unity 打开指定路径文件夹
public static void OpenDirectory(string path){
if (string.IsNullOrEmpty(path)) return;
path=path.Replace("/", "\\");
if (!Directory.Exists(path)){
Debug.LogError("No Directory: " + path);
return;
}
//可能360不信任
System.Diagnostics.Process.Start("explorer.exe", path);
}
public static void OpenDirectory(string path){
// 新开线程防止锁死
Thread newThread=new Thread(new ParameterizedThreadStart(CmdOpenDirectory));
newThread.Start(path);
}
private static void CmdOpenDirectory(object obj){
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c start " + obj.ToString();
UnityEngine.Debug.Log(p.StartInfo.Arguments);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
p.Close();
}
支持MAC
public static void OpenDirectory(string path){
if (string.IsNullOrEmpty(path)) return;
if (!Directory.Exists(path)){
UnityEngine.Debug.LogError("No Directory: " + path);
return;
}
//Application.dataPath 只能在主线程中获取
int lastIndex = Application.dataPath.LastIndexOf("/");
shellPath = Application.dataPath.Substring(0, lastIndex) + "/Shell/";
// 新开线程防止锁死
Thread newThread = new Thread(new ParameterizedThreadStart(CmdOpenDirectory));
newThread.Start(path);
}
private static void CmdOpenDirectory(object obj){
Process p = new Process();
#if UNITY_EDITOR_WIN
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c start " + obj.ToString();
#elif UNITY_EDITOR_OSX
p.StartInfo.FileName = "bash";
string shPath = shellPath + "openDir.sh";
p.StartInfo.Arguments = shPath + " " + obj.ToString();
#endif
//UnityEngine.Debug.Log(p.StartInfo.Arguments);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
p.Close();
}
openDir.sh
#!/bin/bash
tempDirectory=$*
echo "${tempDirectory}"
open "${tempDirectory}"