现在的项目里一键打APK和打ab包后,都需要手动去打开输出目录,稍微有点烦,就加了个打包完后自动弹出输出目录功能。

        代码其实很简单:

[MenuItem(“ZP/Test”)]
public static void Test()
{
string output = @”D:/ZP/Test”;
if (!Directory.Exists(output))
{
Directory.CreateDirectory(output);
}
output = output.Replace(“/”, “\\”);
System.Diagnostics.Process.Start(“explorer.exe”, output);
}
        封装一下,就可以用到很多地方:

public static void OpenDirectory(string path)
{
if (string.IsNullOrEmpty(path)) return;

path = path.Replace(“/”, “\\”);
if (!Directory.Exists(path))
{
Debug.LogError(“No Directory: ” + path);
return;
}

System.Diagnostics.Process.Start(“explorer.exe”, path);
}
        *近在用到这个接口的时候发现不是很好用,因为会被360监听成不信任,所以改成用cmd的start命令来实现,这样每次都不会弹出是否信任Unity的界面了。

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上发现没有这功能,也很烦躁,又看了下Shell,就在增加了个功能支持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:

tempDirectory=$1
echo ${tempDirectory}
open ${tempDirectory}
      *近在Mac上使用的时候发现不能打开带有空格的路径,比如持久化目录,因此对sh文件稍作修改:

#!/bin/bash

tempDirectory=$*

echo “${tempDirectory}”
open “${tempDirectory}”