StandaloneFileBrowserEditor.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEditor;
  6. namespace SFB
  7. {
  8. public class StandaloneFileBrowserEditor : IStandaloneFileBrowser<ItemWithStream>
  9. {
  10. public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
  11. {
  12. string path = "";
  13. if (extensions == null)
  14. {
  15. path = EditorUtility.OpenFilePanel(title, directory, "");
  16. }
  17. else
  18. {
  19. path = EditorUtility.OpenFilePanelWithFilters(title, directory, GetFilterFromFileExtensionList(extensions));
  20. }
  21. var itemWithStream = new ItemWithStream
  22. {
  23. Name = path//,
  24. //Stream = File.OpenRead(path)
  25. };
  26. return new [] {itemWithStream};
  27. }
  28. public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
  29. {
  30. cb(OpenFilePanel(title, directory, extensions, multiselect));
  31. }
  32. public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
  33. {
  34. var path = EditorUtility.OpenFolderPanel(title, directory, "");
  35. var itemWithStream = new ItemWithStream
  36. {
  37. Name = path//,
  38. //Stream = File.OpenRead(path)
  39. };
  40. return new [] { itemWithStream };
  41. }
  42. public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
  43. {
  44. cb(OpenFolderPanel(title, directory, multiselect));
  45. }
  46. public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
  47. {
  48. var ext = extensions != null ? extensions[0].Extensions[0] : "";
  49. var name = string.IsNullOrEmpty(ext) ? defaultName : defaultName + "." + ext;
  50. var path = EditorUtility.SaveFilePanel(title, directory, name, ext);
  51. var itemWithStream = new ItemWithStream
  52. {
  53. Name = path//,
  54. //Stream = File.OpenRead(path)
  55. };
  56. return itemWithStream;
  57. }
  58. public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
  59. {
  60. cb(SaveFilePanel(title, directory, defaultName, extensions));
  61. }
  62. private static string[] GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
  63. {
  64. var filters = new string[extensions.Length * 2];
  65. for (int i = 0; i < extensions.Length; i++)
  66. {
  67. filters[i * 2] = extensions[i].Name;
  68. filters[i * 2 + 1] = string.Join(",", extensions[i].Extensions);
  69. }
  70. return filters;
  71. }
  72. }
  73. }
  74. #endif