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

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

我们点进打开文件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);
    }
}

接下来直接调用就可以了

 /// <summary>
    /// 打开windows保存窗口
    /// </summary>
    /// <returns>路径名(带名字)</returns>
    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;
        }


    }

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

/// <summary>
    /// 确定保存
    /// </summary>
    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("取消保存");
        }
        
    }

具体代码就这样

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值