Unity选择文件或者文件路径的方法
一、引用对应的库文件:Unity选择文件或路径的库文件.rar
二、编写对应的控制脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | /*** * 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 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /*** * 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 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | /*** * 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 } |
三、测试方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /*** * 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 } |
四、效果图如下
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 我与微信审核的“相爱相杀”看个人小程序副业