You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中EditorUtility.OpenFilePanel能否访问隐藏文件夹?求替代方案

解决Unity Editor中访问隐藏文件夹(如Application.persistentDataPath)的问题

方案1:开启系统文件浏览器的隐藏文件显示

EditorUtility.OpenFilePanel调用的是系统原生文件浏览器,只要开启系统的“显示隐藏文件”选项,就能直接看到Application.persistentDataPath这类隐藏目录:

  • Windows:在文件浏览器中按下Ctrl + Shift + .快速切换显示隐藏文件;或者点击顶部“查看”选项卡,勾选“隐藏的项目”。
  • macOS:按下Cmd + Shift + .临时显示隐藏文件;如果需要永久开启,打开终端执行defaults write com.apple.finder AppleShowAllFiles YES,然后重启Finder(右键Dock的Finder图标选“重新启动”)。

方案2:自定义EditorWindow文件浏览器

如果不想依赖系统设置,可以自己在EditorWindow里实现一个针对Application.persistentDataPath的文件浏览器,直接读取目录内容,示例代码如下:

using UnityEditor;
using UnityEngine;
using System.IO;

public class PersistentFileBrowser : EditorWindow
{
    private string[] _files;
    private Vector2 _scrollPos;

    [MenuItem("Window/Persistent Data Browser")]
    public static void LaunchWindow()
    {
        GetWindow<PersistentFileBrowser>("Persistent Data Browser");
    }

    private void OnEnable()
    {
        RefreshFileList();
    }

    private void RefreshFileList()
    {
        string persistentDir = Application.persistentDataPath;
        _files = Directory.Exists(persistentDir) ? Directory.GetFiles(persistentDir) : new string[0];
    }

    private void OnGUI()
    {
        GUILayout.Label($"Persistent Data Path: {Application.persistentDataPath}", EditorStyles.boldLabel);
        
        if (GUILayout.Button("Refresh Files"))
        {
            RefreshFileList();
        }

        _scrollPos = GUILayout.BeginScrollView(_scrollPos);
        foreach (string filePath in _files)
        {
            string fileName = Path.GetFileName(filePath);
            if (GUILayout.Button(fileName))
            {
                // 这里添加选中文件后的逻辑,比如打开、导入到项目等
                Debug.Log($"Selected file: {filePath}");
                EditorUtility.RevealInFinder(filePath); // 用系统浏览器定位该文件
            }
        }
        GUILayout.EndScrollView();
    }
}

把这段代码放到Editor目录下,就能在Unity顶部菜单的Window里找到Persistent Data Browser,直接浏览persistent路径下的文件。

方案3:直接指定初始路径打开面板

调用EditorUtility.OpenFilePanel时,把初始目录直接设为Application.persistentDataPath,即使面板默认不显示隐藏文件夹,系统也会直接定位到该路径,你可以直接选择里面的文件:

string selectedPath = EditorUtility.OpenFilePanel(
    "Select File from Persistent Path", 
    Application.persistentDataPath, 
    "*" // 可根据需要设置文件筛选器,比如"txt,json"
);

if (!string.IsNullOrEmpty(selectedPath))
{
    // 处理选中的文件逻辑
}

内容的提问来源于stack exchange,提问作者Nic Foster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 04:35:19