更全的链接:Unity 选择本地目录选择文件打开指定文件夹(无需引入dll)_love and share-CSDN博客https://blog.csdn.net/qq_39342142/article/details/103781728
using UnityEngine; using System.Collections; using System; using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFile { ? ? 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 WindowDll { ? ? [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] ? ? public static extern bool GetOpenFileName([In, Out] OpenFile of); ? ? public static bool GetOpenFileName1([In, Out] OpenFile of) ? ? { ? ? ? ? return GetOpenFileName(of); ? ? } ? ? /// <summary> ? ? /// 打开文件 ? ? /// </summary> ? ? /// <param name="filter">过滤器</param> ? ? /// <param name="m_defaultPath">默认打开的路径</param> ? ? /// <returns></returns> ? ? public static string OpenFile(string filter,string m_defaultPath) ? ? { ? ? ? ? OpenFile of = new OpenFile(); ? ? ? ? of.structSize = Marshal.SizeOf(of); ? ? ? ? of.filter = filter; ? ? ? ? of.file = new string(new char[256]); ? ? ? ? of.maxFile = of.file.Length; ? ? ? ? of.fileTitle = new string(new char[64]); ? ? ? ? of.maxFileTitle = of.fileTitle.Length; ? ? ? ? string defaultPath = m_defaultPath; ? ? ? ? defaultPath = defaultPath.Replace('/', '\\'); ? ? ? ? of.initialDir = defaultPath; ? ? ? ? of.title = "小宝贝儿,Word很大你忍一下"; ? ? ? ? of.defExt = "DOCX"; ? ? ? ? of.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; ? ? ? ? if (WindowDll.GetOpenFileName(of)) ? ? ? ? { ? ? ? ? ? ? Debug.Log("Selected file with full path: {0}" + of.file); ? ? ? ? ? ? return of.file; ? ? ? ? } ? ? ? ? return string.Empty; ? ? } }
|