一、引用對應的庫文件:Unity選擇文件或路徑的庫文件.rar
二、編寫對應的控制腳本
/*** * Title:"智慧工廠" 項目 * 主題:打開文件 * Description: * 功能:XXX * Date:2019 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using UnityEngine; using System.Collections; using System; using System.Runtime.InteropServices; namespace kernal { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class OpenSelectFileName { 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; }//Class_end }
/*** * Title:"智慧工廠" 項目 * 主題:本地彈窗 * Description: * 功能:XXX * Date:2019 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using System.Runtime.InteropServices; namespace Kernal { public class LocalDialog { //鏈接指定系統函數 打開文件對話框 [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern bool GetOpenFileName([In, Out] OpenSelectFileName ofn); public static bool GetOFN([In, Out] OpenSelectFileName ofn) { return GetOpenFileName(ofn); } //鏈接指定系統函數 另存為對話框 [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern bool GetSaveFileName([In, Out] OpenSelectFileName ofn); public static bool GetSFN([In, Out] OpenSelectFileName ofn) { return GetSaveFileName(ofn); } }//Class_end }
/*** * Title:"智慧工廠" 項目 * 主題:選擇資源或者路徑方法 * Description: * 功能:XXX * Date:2019 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using System.Runtime.InteropServices; using System.Windows.Forms; namespace kernal { public class SelectFileOrPath { private static SelectFileOrPath _Instance; //本類實例 #region 屬性 private string _SelectFilePath; //選擇的文件路徑 private string _SelectFile; //選擇文件 private string _FileName; //文件名稱 //選擇文件路徑 public string SelectFilePath { get { return _SelectFilePath; } set { _SelectFilePath = value; } } //選擇文件 public string SelectFile { get { return _SelectFile; } set { _SelectFile = value; } } //文件名稱 public string FileName { get { return _FileName; } set { _FileName = value; } } #endregion /// <summary> /// 本類實例 /// </summary> /// <returns></returns> public static SelectFileOrPath GetInstance() { if (_Instance==null) { _Instance = new SelectFileOrPath(); } return _Instance; } /// <summary> /// 選擇文件路徑(Unity自帶的方法) /// </summary> public void SelectFolderPath_Unity() { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { SelectFilePath = fbd.SelectedPath; } } /// <summary> /// 選擇文件(Unity自帶的方法) /// </summary> public void SelectFileSelf_Unity() { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = "file://" + UnityEngine.Application.dataPath;//默認打開路徑 if (ofd.ShowDialog() == DialogResult.OK) { SelectFile = ofd.FileName; } } /// <summary> /// 選擇文件路徑(調用Windows方法) /// </summary> public void SelectFilePath_Windows() { OpenSelectFileName openFileName = new OpenSelectFileName(); openFileName.structSize = Marshal.SizeOf(openFileName); openFileName.filter = "PDF文件(*.PDF)\0*.PDF|數據庫文件(*.bak)\0*.bak|*.*\0*.*"; openFileName.file = new string(new char[256]); openFileName.maxFile = openFileName.file.Length; openFileName.fileTitle = new string(new char[64]); openFileName.maxFileTitle = openFileName.fileTitle.Length; openFileName.initialDir = UnityEngine.Application.streamingAssetsPath.Replace('/', '\\');//默認路徑 openFileName.title = "請選擇路徑或者文件"; openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008; if (LocalDialog.GetSaveFileName(openFileName)) { SelectFilePath = openFileName.file; FileName= openFileName.file; } } }//Class_end }
三、測試方法
/*** * Title:"智慧工廠" 項目 * 主題:測試選擇路徑或文件 * Description: * 功能:XXX * Date:2019 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using System.Collections; using System.Collections.Generic; using UnityEngine; using Control; using kernal; namespace SimpleUIFrame { public class Test_BackUpSqlServerDatabase : MonoBehaviour { private string _BackUpPath; //備份路徑 void Start() { } private void Update() { if (Input.GetKeyDown(KeyCode.X)) { bool isSuccess = false; //選擇備份路徑 SelectFileOrPath.GetInstance().SelectFilePath_Windows(); _BackUpPath = SelectFileOrPath.GetInstance().SelectFilePath; if (!string.IsNullOrEmpty(_BackUpPath)) { Debug.Log("備份文件的路徑=" + _BackUpPath); } }//Class_end }
四、效果圖如下