StandaloneFileBrowserEditor.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEditor;
  6. namespace TriLibCore.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. };
  25. return new [] {itemWithStream};
  26. }
  27. public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
  28. {
  29. cb(OpenFilePanel(title, directory, extensions, multiselect));
  30. }
  31. public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
  32. {
  33. var path = EditorUtility.OpenFolderPanel(title, directory, "");
  34. var itemWithStream = new ItemWithStream
  35. {
  36. Name = path
  37. };
  38. return new [] { itemWithStream };
  39. }
  40. public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
  41. {
  42. cb(OpenFolderPanel(title, directory, multiselect));
  43. }
  44. public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
  45. {
  46. var ext = extensions != null ? extensions[0].Extensions[0] : "";
  47. var name = string.IsNullOrEmpty(ext) ? defaultName : defaultName + "." + ext;
  48. var path = EditorUtility.SaveFilePanel(title, directory, name, ext);
  49. var itemWithStream = new ItemWithStream
  50. {
  51. Name = path
  52. };
  53. return itemWithStream;
  54. }
  55. public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
  56. {
  57. cb(SaveFilePanel(title, directory, defaultName, extensions));
  58. }
  59. private static string[] GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
  60. {
  61. var filters = new string[extensions.Length * 2];
  62. for (int i = 0; i < extensions.Length; i++)
  63. {
  64. filters[i * 2] = extensions[i].Name;
  65. filters[i * 2 + 1] = string.Join(",", extensions[i].Extensions);
  66. }
  67. return filters;
  68. }
  69. }
  70. }
  71. #endif