1 概述
本文鏈接:http://blog.csdn.net/ithot/article/details/76997237 用Unity以來(lái),一直都沒(méi)怎么關(guān)注過(guò)打開(kāi)對(duì)話框去選取本地文件,最近需要用到這個(gè)功能,在網(wǎng)上搜索了一下,在搜索的資料的基礎(chǔ)上,對(duì)比下Unity運(yùn)行時(shí)打開(kāi)文件對(duì)話框的兩種方法,以及編輯時(shí)的打開(kāi)文件Windows對(duì)話框方法。有些地方不太懂,期待路過(guò)的大神指導(dǎo)下。
工程文件下載(Unity 2017.1.0f3版本):鏈接: http://pan.baidu.com/s/1skJ8yvj 密碼: f97k
2 運(yùn)行時(shí)打開(kāi)文件對(duì)話框
2.1 引用System.Windows.Forms.dll方法
這種方法是通過(guò)直接把System.Windows.Forms.dll拷貝到Plugins目錄下,然后在代碼中引用,調(diào)用Windows的打開(kāi)文件對(duì)話框方法來(lái)實(shí)現(xiàn)。
實(shí)現(xiàn)步驟:
1.打開(kāi)Unity的安裝目錄,在安裝目錄下搜索System.Windows.Forms.dll,搜索結(jié)果中會(huì)有多個(gè)同名的dll,選擇版本為2.0,且文件大小較大的那個(gè)(由于Editor只能使用.NET 3.5或更低版本,所以4.0和4.5版本都是無(wú)法使用,我也從.NET的安裝目錄下拷貝了3.5和3.0版本的進(jìn)行測(cè)試,但是都會(huì)出問(wèn)題,而且不允許被打包使用,不知道為什么)
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"; //過(guò)濾文件類(lèi)型 dialog.InitialDirectory = "D:\\"; //定義打開(kāi)的默認(rèn)文件夾位置,可以在顯示對(duì)話框之前設(shè)置好各種屬性 if (dialog.ShowDialog() == DialogResult.OK) { Debug.Log(dialog.FileName); } } } 4. 在Unity中添加一個(gè)Button,給button的點(diǎn)擊事件添加方法
實(shí)現(xiàn)效果:
這個(gè)對(duì)話框有點(diǎn)丑,感覺(jué)就像是上世紀(jì)的老古董,我估計(jì)是因?yàn)?.0版本的原因,如果能使用高版本的Forms的dll,應(yīng)該是能和目前win10的打開(kāi)對(duì)話框的樣式保持一致的
2.2 調(diào)用Win32打開(kāi)對(duì)話框
這一塊沒(méi)搞太懂,我初步認(rèn)為是編寫(xiě)一個(gè)對(duì)話框類(lèi),然后通過(guò)運(yùn)行時(shí)調(diào)用Windows應(yīng)用程序公用對(duì)話框模塊Comdlg32.dll,來(lái)打開(kāi)對(duì)話框
實(shí)現(xiàn)步驟:
1. 添加FileOpenDialog類(lèi)
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); //這個(gè)方法名稱(chēng)必須為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; //默認(rèn)路徑 dialog.title = "Open File Dialog"; dialog.defExt = "exe";//顯示文件的類(lèi)型 //注意一下項(xiàng)目不一定要全選 但是0x00000008項(xiàng)不要缺少 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中添加一個(gè)Button,給button的點(diǎn)擊事件添加OpenFileByWin32中的OpenFile()方法 實(shí)現(xiàn)效果:
這個(gè)對(duì)話框就和平常打開(kāi)的Windows上的對(duì)話框是一致的,而且也不需要再找dll導(dǎo)入,所以我更傾向于使用這種方法。
3 編輯時(shí)打開(kāi)文件對(duì)話框
在編輯時(shí)Editor有自己的類(lèi)可以非常方便的打開(kāi)文件對(duì)話框或者文件夾對(duì)話框,只需使用EditorUtility.OpenFilePanel或者EditorUtility.OpenFolderPanel方法即可
實(shí)現(xiàn)代碼:
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); } } 實(shí)現(xiàn)效果:
|