private static string[] openFoldersNew(string directory) { if (Util.Config.DEBUG && !string.IsNullOrEmpty(directory)) { Debug.LogWarning("'directory' is not supported under Windows."); } NativeMethods.IFileOpenDialog dialog = (NativeMethods.IFileOpenDialog) new NativeMethods.FileOpenDialog(); dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); //dialog.SetOptions(FOS.FOS_PICKFOLDERS); try { NativeMethods.IShellItem item; /* * if (!string.IsNullOrEmpty(DirectoryPath)) * { * IntPtr idl = IntPtr.Zero; * uint atts = 0; * if (SHILCreateFromPath(DirectoryPath, out idl, ref atts) == 0) * { * if (SHCreateShellItem(System.IntPtr.Zero, System.IntPtr.Zero, idl, out item) == 0) * { * dialog.SetFolder(item); //crashes Unity! * } * } * * System.Runtime.InteropServices.Marshal.FreeCoTaskMem(idl); * } */ uint hr = dialog.Show(currentWindow); if (hr == 0) { dialog.GetResult(out item); item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out string path); return(new[] { path }); } } catch (Exception ex) { Debug.LogError($"Folder dialog threw an error: {ex}"); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(dialog); } return(new string[0]); }
static void SetFileTypes(PickOptions options, NativeMethods.IFileOpenDialog dialog) { var hasAtLeastOneType = false; var filters = new List <NativeMethods.COMDLG_FILTERSPEC>(); if (options?.FileTypes == FilePickerFileType.Images) { var types = new StringBuilder(); foreach (var type in options.FileTypes.Value) { types.Append(type + ";"); } filters.Add(new NativeMethods.COMDLG_FILTERSPEC { pszName = "Images\0", pszSpec = types.ToString() + "\0" }); hasAtLeastOneType = true; } else if (options?.FileTypes == FilePickerFileType.Videos) { var types = new StringBuilder(); foreach (var type in options.FileTypes.Value) { types.Append(type + ";"); } filters.Add(new NativeMethods.COMDLG_FILTERSPEC { pszName = "Videos\0", pszSpec = types.ToString() + "\0" }); hasAtLeastOneType = true; } else if (options?.FileTypes?.Value != null) { foreach (var type in options.FileTypes.Value) { if (type.StartsWith(".") || type.StartsWith("*.")) { filters.Add(new NativeMethods.COMDLG_FILTERSPEC { pszName = type.TrimStart('*') + "\0", pszSpec = type + "\0" }); hasAtLeastOneType = true; } } } if (hasAtLeastOneType) { dialog.SetFileTypes(filters.Count, filters.ToArray()); } }