首页 > 编程语言 > 详细

unity 打开指定路径文件夹

时间:2019-09-28 13:26:36      阅读:625      评论:0      收藏:0      [点我收藏+]
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

unity 打开指定路径文件夹

原文:https://www.cnblogs.com/kingBook/p/11602507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!