Unity | 打开文件对话框批量选择文件

本文介绍了在不同环境下实现批量选择文件的三种方法,包括使用System.Windows.Forms.OpenFileDialog、通过OpenFileName结构调用Comdlg32.dll以及在Unity编辑器中使用EditorUtility。每种方法的优缺点及适用场景也被详细讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前在新浪博客写了一篇关于打开文件对话框批量选择文件的文章,可惜新浪博客不能写代码,奈何当时太年轻,并不觉得不方便,直到遇到CSDN...emmm,不想将就了,所以在这里更新一波,并补充其他的方法:

  • 原文的方法一

1. 源码如下:

            System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
            dlg.Multiselect = true;
            dlg.RestoreDirectory = false;
            dlg.ValidateNames = true;
            dlg.FilterIndex = 1;
            dlg.Filter = "图片文件(*.jpg;*.png;*.jpeg)|*.jpg;*.png;*.jpeg|pdf文件|*.pdf|所有文件|*."; //"文本文件|*.*|C#文件|*.cs|所有文件|*.*";// "图片文件或PDF文件(*.jpg;*.png;*.jpeg;*.pdf)|*.jpg;*.png;*.jpeg;*.pdf";
            dlg.InitialDirectory = "C:\\";
            dlg.Title = "请选择要识别的图片或pdf";

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                for (int i = 0; i < dlg.FileNames.Length; i++)
                {
                    Debug.Log(dlg.FileNames.GetValue(i).ToString());
                }
            }

2. 缺点:窗口风格旧、打包后运行有问题(看不到文件夹):

  • 原文的方法二

1. 源码如下:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
    public int structSize;
    public IntPtr dlgOwner;
    public IntPtr instance;
    public string filter;
    public string customFilter;
    public int maxCustFilter;
    public int filterIndex;
    public string file;
    public int maxFile;
    public string fileTitle;
    public int maxFileTitle;
    public string initialDir;  //打开路径     null
    public string title;
    public int flags;
    public short fileOffset;
    public short fileExtension;
    public string defExt;
    public IntPtr custData;
    public IntPtr hook;
    public string templateName;
    public IntPtr reservedPtr;
    public int reservedInt;
    public int flagsEx;
}
    void OpenOfnInit()
    {
        Openofn = new OpenFileName();
        Openofn.structSize = Marshal.SizeOf(Openofn);
        Openofn.dlgOwner = DllScript.GetForegroundWindow();
        Openofn.filter = "图片文件或PDF文件(*.jpg;*.png;*.jpeg;*.pdf)\0*.jpg;*.png;*.jpeg;*.pdf\0";
        Openofn.file = new string(new char[1024]);
        Openofn.maxFile = Openofn.file.Length;
        Openofn.fileTitle = new string(new char[64]);
        Openofn.maxFileTitle = Openofn.fileTitle.Length;
        Openofn.initialDir = "C:\\";
        Openofn.title = "选择图片或PDF文件";
        Openofn.defExt = "PDF";
        Openofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;   //OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR    
    }
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
if (GetOpenFileName(Openofn))
{
    string[] SplitStr = { "\0" };
    string[] strs = Openofn.file.Split(SplitStr, StringSplitOptions.RemoveEmptyEntries);
    for (int i = 0; i < strs.Length; i++)
    {
        Debug.Log(strs[i]);
    }
}

2. 缺点:发布32位时只能单选文件:去掉0x00000200;

  • 方法三:只能在Unity 编辑器窗口内使用
  string[] fliter = { "图片或pdf", "jpg;*.png;*.jpeg;*.pdf" };
  string path = UnityEditor.EditorUtility.OpenFilePanelWithFilters("Overwrite with png", "C:\\", fliter);
  • 针对方法一,补充一个打开文件夹的方法:
            System.Windows.Forms.FolderBrowserDialog _dlg = new System.Windows.Forms.FolderBrowserDialog();

            if (_dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Debug.Log(_dlg.SelectedPath);

            }

 

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烫青菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值