Unity中打开对话框选择文件获取到文件完整路径(Comdlg32.dll)

开发中遇到客户需要自定义程序背景和背景音乐的,所以就有了需要打开对话框让客户自己去选择对应文件,程序只需要记录下来用户选择的路径就可以了。

参考资料:https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/w5tyztk9(v=vs.100)?redirectedfrom=MSDN#calling-functions

https://msdn.microsoft.com/zh-cn/visualc/ms646927(v=vs.80)

unity打开本地文件

引用Comdlg32.dll系统类库,GetOpenFileName方法,下面看代码,

 public class OpenDialog
    {
    
    
 
        //链接指定系统函数       打开文件对话框
        [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
        public static bool GetOFN([In, Out] OpenFileName ofn)
        {
    
    
            return GetOpenFileName(ofn);
        }
        //链接指定系统函数        另存为对话框
        [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
        public static bool GetSFN([In, Out] OpenFileName ofn)
        {
    
    
            return GetSaveFileName(ofn);
        }
    }

下面是OpenFileName脚本,

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class OpenFileName
    {
    
    
        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 enum FileType
    {
    
    
        None,
        Text,
        Texture,
        Video,
        Music,
    }
    /// <summary>
    /// 文件操作类工具
    /// </summary>
    public static class FileIOUtil
    {
    
     
        /// <summary>
        /// 打开一个窗口选择一个文件
        /// </summary>
        /// <param name="fileType">需要选择的文件类型</param>
        /// <param name="openPath">设置打开的默认路径</param>
        /// <returns>返回选择文件的路径</returns>
        public static string OpenFileDialog(FileType fileType, string openPath = null)
        {
    
    
            OpenFileName ofn = new OpenFileName();
            ofn.structSize = Marshal.SizeOf(ofn);
            string fliter = string.Empty;
            switch (fileType)
            {
    
    
                case FileType.None:
                    fliter = "All Files(*.*)\0*.*\0\0";
                    break;
                case FileType.Text:
                    fliter = "Text Files(*文本文件)\0*.txt\0";
                    break;
                case FileType.Texture:
                    fliter = "Texure Files(*图片文件)\0*.png;*.jpg\0";
                    break;
                case FileType.Video:
                    fliter = "Video Files(*视频文件)\0*.mp4;*.mov\0";
                    break;
                case FileType.Music:
                    fliter = "Music Files(*音频文件)\0*.wav;*.mp3\0";
                    break;
            }
            ofn.filter = fliter;//设置需要选择的类型
            ofn.file = new string(new char[256]);
            ofn.maxFile = ofn.file.Length;
            ofn.fileTitle = new string(new char[64]);
            ofn.maxFileTitle = ofn.fileTitle.Length;
            if (string.IsNullOrEmpty(openPath))
                ofn.initialDir = System.Environment.CurrentDirectory; //打开的默认路径自行更改
            else
                ofn.initialDir = openPath;
            ofn.title = "选择文件";//标题 自定义 自行更改
            //0x00000200  设置用户可以选择多个文件 不使用 暂时没找到解析多个文件路径的解决方法
            //具体含义查看给到的参考文献链接
            ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
            if (OpenDialog.GetOFN(ofn))
            {
    
    
                Debug.Log(ofn.file);
                return ofn.file;
            }
            return null;
        }
    }

开发中遇到客户需要自定义程序背景和背景音乐的,所以就有了需要打开对话框让客户自己去选择对应文件,程序只需要记录下来用户选择的路径就可以了。

参考资料:https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/w5tyztk9(v=vs.100)?redirectedfrom=MSDN#calling-functions

https://msdn.microsoft.com/zh-cn/visualc/ms646927(v=vs.80)

猜你喜欢

转载自blog.csdn.net/m1234567q/article/details/127119940