FileDlg.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using UnityEngine;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  5. public class OpenFileName
  6. {
  7. #region Config Field
  8. public int structSize = 0;
  9. public IntPtr dlgOwner = IntPtr.Zero;
  10. public IntPtr instance = IntPtr.Zero;
  11. public String filter = null;
  12. public String customFilter = null;
  13. public int maxCustFilter = 0;
  14. public int filterIndex = 0;
  15. public String file = null;
  16. public int maxFile = 0;
  17. public String fileTitle = null;
  18. public int maxFileTitle = 0;
  19. public String initialDir = null;
  20. public String title = null;
  21. public int flags = 0;
  22. public short fileOffset = 0;
  23. public short fileExtension = 0;
  24. public String defExt = null;
  25. public IntPtr custData = IntPtr.Zero;
  26. public IntPtr hook = IntPtr.Zero;
  27. public String templateName = null;
  28. public IntPtr reservedPtr = IntPtr.Zero;
  29. public int reservedInt = 0;
  30. public int flagsEx = 0;
  31. #endregion
  32. #region Win32API WRAP
  33. [DllImport("user32.dll")]
  34. static extern IntPtr GetForegroundWindow();
  35. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  36. static extern bool GetOpenFileName([In, Out] LocalDialog dialog); //这个方法名称必须为GetOpenFileName
  37. //[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  38. //static extern bool GetSaveFileName([In, Out] LocalDialog dialog); //这个方法名称必须为GetSaveFileName
  39. [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  40. public static extern IntPtr SHBrowseForFolder([In, Out] OpenFileName ofn);
  41. [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  42. public static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
  43. #endregion
  44. }
  45. public class LocalDialog
  46. {
  47. public String DisplayName = null;
  48. public String Title = null;
  49. //链接指定系统函数 打开文件对话框
  50. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  51. public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
  52. //public static bool GetOFN([In, Out] OpenFileName ofn)
  53. //{
  54. // return GetOpenFileName(ofn);
  55. //}
  56. //窗口置顶
  57. [DllImport("user32.dll")]
  58. public static extern IntPtr GetForegroundWindow();
  59. /// <summary>
  60. /// 调用window原生,获取存储的文件夹
  61. /// </summary>
  62. /// <param name="title">提示信息</param>
  63. /// <param name="callback">选择后的回调方法</param>
  64. public static void GetStorageFolderName(string title, Action<string> callback = null)
  65. {
  66. OpenFileName ofn = new OpenFileName();
  67. ofn.file = new string(new char[2000]); ; // 存放目录路径缓冲区
  68. ofn.title = title;// 标题
  69. IntPtr pidlPtr = OpenFileName.SHBrowseForFolder(ofn);
  70. char[] charArray = new char[2000];
  71. for (int i = 0; i < 2000; i++)
  72. charArray[i] = '\0';
  73. ofn.dlgOwner = LocalDialog.GetForegroundWindow(); //这一步将文件选择窗口置顶。
  74. OpenFileName.SHGetPathFromIDList(pidlPtr, charArray);
  75. string fullDirPath = new String(charArray);
  76. fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
  77. if (callback!=null)
  78. {
  79. Debug.Log(fullDirPath);
  80. callback(fullDirPath);//这个就是选择的目录路径。
  81. }
  82. }
  83. /// <summary>
  84. /// 调用window原生,获取选择的文件
  85. /// </summary>
  86. /// <param name="title"> 提示信息 </param>
  87. /// <param name="filter"> "所有格式(*;)\0*;" </param>
  88. /// <param name="callback">选择后的回调方法 </param>
  89. public static void GetLocalFileName(string title, string filter, Action<string> callback = null)
  90. {
  91. OpenFileName ofn = new OpenFileName();
  92. ofn.structSize = Marshal.SizeOf(ofn);
  93. ofn.filter = filter;
  94. ofn.file = new string(new char[2000]);
  95. ofn.maxFile = ofn.file.Length;
  96. ofn.fileTitle = new string(new char[64]);
  97. ofn.maxFileTitle = ofn.fileTitle.Length;
  98. ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
  99. ofn.title = title;
  100. //注意 一下项目不一定要全选 但是0x00000008项不要缺少
  101. //OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
  102. ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
  103. ofn.dlgOwner = LocalDialog.GetForegroundWindow(); //这一步将文件选择窗口置顶。
  104. if (LocalDialog.GetOpenFileName(ofn))
  105. {
  106. if (callback != null)
  107. {
  108. Debug.Log(ofn.file);
  109. callback(ofn.file);
  110. }
  111. }
  112. }
  113. }