Unity中打开文件窗口(OpenFileDialog)的几种方法对比

1 概述

本文链接:http://blog.csdn.net/ithot/article/details/76997237
用Unity以来,一直都没怎么关注过打开对话框去选取本地文件,最近需要用到这个功能,在网上搜索了一下,在搜索的资料的基础上,对比下Unity运行时打开文件对话框的两种方法,以及编辑时的打开文件Windows对话框方法。有些地方不太懂,期待路过的大神指导下。

工程文件下载(Unity 2017.1.0f3版本):链接: http://pan.baidu.com/s/1skJ8yvj 密码: f97k


2 运行时打开文件对话框

2.1 引用System.Windows.Forms.dll方法

这种方法是通过直接把System.Windows.Forms.dll拷贝到Plugins目录下,然后在代码中引用,调用Windows的打开文件对话框方法来实现。

实现步骤:

1.打开Unity的安装目录,在安装目录下搜索System.Windows.Forms.dll,搜索结果中会有多个同名的dll,选择版本为2.0,且文件大小较大的那个(由于Editor只能使用.NET 3.5或更低版本,所以4.0和4.5版本都是无法使用,我也从.NET的安装目录下拷贝了3.5和3.0版本的进行测试,但是都会出问题,而且不允许被打包使用,不知道为什么)

2. 在Unity的Projects目下新建Plugins文件夹,拷贝System.Windows.Forms.dll到该目录

3. 在代码中引用该dll,并添加如下代码:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Windows.Forms;
 
public class OpenFileByDll : MonoBehaviour {
 
    public void OpenFile()
    {
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "exe files (*.exe)|*.exe";  //过滤文件类型
        dialog.InitialDirectory = "D:\\";  //定义打开的默认文件夹位置,可以在显示对话框之前设置好各种属性
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            Debug.Log(dialog.FileName);
        }
    }
}
4. 在Unity中添加一个Button,给button的点击事件添加方法

实现效果:

这个对话框有点丑,感觉就像是上世纪的老古董,我估计是因为2.0版本的原因,如果能使用高版本的Forms的dll,应该是能和目前win10的打开对话框的样式保持一致的


 

2.2 调用Win32打开对话框

这一块没搞太懂,我初步认为是编写一个对话框类,然后通过运行时调用Windows应用程序公用对话框模块Comdlg32.dll,来打开对话框

实现步骤:

1. 添加FileOpenDialog类


using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
 
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FileOpenDialog
{
    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 DialogShow
{
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out]FileOpenDialog dialog);  //这个方法名称必须为GetOpenFileName
}
2. 添加OpenFileByWin32脚本


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
 
public class OpenFileByWin32 : MonoBehaviour
{
    public void OpenFile()
    {
        FileOpenDialog dialog = new FileOpenDialog();
 
        dialog.structSize = Marshal.SizeOf(dialog);
 
        dialog.filter = "exe files\0*.exe\0All Files\0*.*\0\0";
 
        dialog.file = new string(new char[256]);
 
        dialog.maxFile = dialog.file.Length;
 
        dialog.fileTitle = new string(new char[64]);
 
        dialog.maxFileTitle = dialog.fileTitle.Length;
 
        dialog.initialDir = UnityEngine.Application.dataPath;  //默认路径
 
        dialog.title = "Open File Dialog";
 
        dialog.defExt = "exe";//显示文件的类型
        //注意一下项目不一定要全选 但是0x00000008项不要缺少
        dialog.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;  //OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
        
        if (DialogShow.GetOpenFileName(dialog))
        {
            Debug.Log(dialog.file);
        }
    }
}
3. 在Unity中添加一个Button,给button的点击事件添加OpenFileByWin32中的OpenFile()方法
实现效果:

这个对话框就和平常打开的Windows上的对话框是一致的,而且也不需要再找dll导入,所以我更倾向于使用这种方法。


3 编辑时打开文件对话框

在编辑时Editor有自己的类可以非常方便的打开文件对话框或者文件夹对话框,只需使用EditorUtility.OpenFilePanel或者EditorUtility.OpenFolderPanel方法即可

实现代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
 
public class FileOpen : MonoBehaviour{
 
    [MenuItem("Custom/OpenFile")]
    public static void OpenFile()
    {
        string file = EditorUtility.OpenFilePanel("Open File Dialog", "D:\\", "exe");
        Debug.Log(file);
    }
 
    [MenuItem("Custom/OpenFolder")]
    public static void OpenFolder()
    {
        string file = EditorUtility.OpenFolderPanel("Open Folder Dialog", "D:\\", "unity");
        Debug.Log(file);
    }
}
实现效果:


参考资料
http://www.cnblogs.com/U-tansuo/archive/2012/07/10/GetOpenFileName.html
————————————————
版权声明:本文为CSDN博主「番茄猿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ithot/article/details/76997237

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值