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

之前在新浪博客写了一篇关于打开文件对话框批量选择文件的文章,可惜新浪博客不能写代码,奈何当时太年轻,并不觉得不方便,直到遇到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);

            }
发布了162 篇原创文章 · 获赞 20 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_39766005/article/details/103705178