1. 程式人生 > >[Unity]Unity開啟資料夾

[Unity]Unity開啟資料夾

        現在的專案裡一鍵打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}