[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}

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

#!/bin/bash

tempDirectory=$*

echo "${tempDirectory}"
open "${tempDirectory}"

 

  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值