[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();
    }

猜你喜欢

转载自blog.csdn.net/zp288105109a/article/details/81366343