使用unity自带API EditorUtility打开本地文件夹或者文件,以及打包后发布打开本地文件夹

首先我们查一下unity官方手册可以看到下面的画面(我是使用谷歌浏览器的翻译)。

使用unity自带API EditorUtility打开本地文件夹或者文件,以及打包后发布打开本地文件夹_第1张图片

我们点进打开文件OpenFilePanel里去看官方示例,代码如下:

using System.IO;
using UnityEngine;
using UnityEditor;

public class OpenFilePanelExample : EditorWindow
{
    [MenuItem("Example/Overwrite Texture")]
    static void Apply()
    {
        Texture2D texture = Selection.activeObject as Texture2D;
        if (texture == null)
        {
            EditorUtility.DisplayDialog("Select Texture", "You must select a texture first!", "OK");
            return;
        }

        string path = EditorUtility.OpenFilePanel("Overwrite with png", "", "png");
        if (path.Length != 0)
        {
            var fileContent = File.ReadAllBytes(path);
            texture.LoadImage(fileContent);
        }
    }
}

再看一下打开文件夹OpenFolderPanel官方示例,代码如下:

using UnityEditor;
using System.IO;

public class OpenFolderPanelExample : EditorWindow
{
    [MenuItem("Example/Load Textures To Folder")]
    static void Apply()
    {
        string path = EditorUtility.OpenFolderPanel("Load png Textures", "", "");
        string[] files = Directory.GetFiles(path);

        foreach (string file in files)
            if (file.EndsWith(".png"))
                File.Copy(file, EditorApplication.currentScene);
    }
}

 

是不是觉得大同小异呢,SaveFilePanel以及SaveFolderPanel也是同样的方式我这里就不演示。

我的应用代码如下:


    void ConfirmBtnClick()
    {
        
        //SceneManager.LoadScene(1);
        string AssetsPath = Application.dataPath + "/Resources/";
        string Path = EditorUtility.SaveFolderPanel("请选择文件夹", AssetsPath, "");
        if (Path.Length != 0)
        {
            Debug.Log("开始保存数据");
            ///TODO  保存数据

            SceneManager.LoadScene(1);
        }
        else
        {
            Debug.Log("取消保存");
        }
        
    }

因为接下来需要保存的数据还没有完成,所以这里只是debug一下。

上面的方法虽然在Unity编辑器里是可以打开文件夹的,但是我们将软件打包是会发现报错无法打包,这是因为该脚本引用using UnityEditor;只能在编辑器下使用,所以我们还是不要想着偷懒老老实实使用类吧,首先我们创建两个类。

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
    public int structSize = 0;
    public IntPtr dlgOwner = IntPtr.Zero;
    public IntPtr instance = IntPtr.Zero;
    public String filter = null;
    public String customFilter = null;
    public int maxCustFilter = 0;
    public int filterIndex = 0;
    public String file = null;
    public int maxFile = 0;
    public String fileTitle = null;
    public int maxFileTitle = 0;
    public String initialDir = null;
    public String title = null;
    public int flags = 0;
    public short fileOffset = 0;
    public short fileExtension = 0;
    public String defExt = null;
    public IntPtr custData = IntPtr.Zero;
    public IntPtr hook = IntPtr.Zero;
    public String templateName = null;
    public IntPtr reservedPtr = IntPtr.Zero;
    public int reservedInt = 0;
    public int flagsEx = 0;
}

以及

public class LocalDialog
{
    //链接指定系统函数       打开文件对话框
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
    public static bool GetOFN([In, Out] OpenFileName ofn)
    {
        return GetOpenFileName(ofn);
    }

    //链接指定系统函数        另存为对话框
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
    public static bool GetSFN([In, Out] OpenFileName ofn)
    {
        return GetSaveFileName(ofn);
    }
}

接下来直接调用就可以了

 /// 
    /// 打开windows保存窗口
    /// 
    /// 路径名(带名字)
    string loadFile()
    {
        OpenFileName openFileName = new OpenFileName();
        openFileName.structSize = Marshal.SizeOf(openFileName);
        openFileName.filter = "All files (*.*)|*.*";
        openFileName.file = new string(new char[256]);
        openFileName.maxFile = openFileName.file.Length;
        openFileName.fileTitle = new string(new char[64]);
        openFileName.maxFileTitle = openFileName.fileTitle.Length;
        openFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
        openFileName.title = "保存项目";
        openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

        if (LocalDialog.GetSaveFileName(openFileName))
        {
            return openFileName.file;

        }
        else
        {
            return null;
        }


    }

因为我需要获取返回的路径名使用,所以我将它写成带返回值的函数。我们实际调用一下试试

/// 
    /// 确定保存
    /// 
    void ConfirmBtnClick()
    {

        //SceneManager.LoadScene(1);
        //string AssetsPath = Application.dataPath + "/Resources/";
        //string Path = UnityEditor.EditorUtility.SaveFolderPanel("请选择文件夹", AssetsPath, "");
        string Path=loadFile();
        if (Path != null)
        {
            if (Path.Length != 0)
            {
                Debug.Log("开始保存数据");
                
            }
        }
        
        else
        {
            Debug.Log("取消保存");
        }
        
    }

具体代码就这样

你可能感兴趣的:(使用unity自带API EditorUtility打开本地文件夹或者文件,以及打包后发布打开本地文件夹)