unity 打开指定路径文件夹

可紊 提交于 2019-11-30 15:53:53
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}"

https://blog.csdn.net/zp288105109a/article/details/81366343

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!