You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.4 KiB

  1. #if UNITY_EDITOR
  2. using System;
  3. using UnityEditor;
  4. namespace SFB {
  5. public class StandaloneFileBrowserEditor : IStandaloneFileBrowser {
  6. public string[] OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect) {
  7. string path = "";
  8. if (extensions == null) {
  9. path = EditorUtility.OpenFilePanel(title, directory, "");
  10. }
  11. else {
  12. path = EditorUtility.OpenFilePanelWithFilters(title, directory, GetFilterFromFileExtensionList(extensions));
  13. }
  14. return string.IsNullOrEmpty(path) ? new string[0] : new[] { path };
  15. }
  16. public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<string[]> cb) {
  17. cb.Invoke(OpenFilePanel(title, directory, extensions, multiselect));
  18. }
  19. public string[] OpenFolderPanel(string title, string directory, bool multiselect) {
  20. var path = EditorUtility.OpenFolderPanel(title, directory, "");
  21. return string.IsNullOrEmpty(path) ? new string[0] : new[] {path};
  22. }
  23. public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<string[]> cb) {
  24. cb.Invoke(OpenFolderPanel(title, directory, multiselect));
  25. }
  26. public string SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions) {
  27. var ext = extensions != null ? extensions[0].Extensions[0] : "";
  28. var name = string.IsNullOrEmpty(ext) ? defaultName : defaultName + "." + ext;
  29. return EditorUtility.SaveFilePanel(title, directory, name, ext);
  30. }
  31. public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<string> cb) {
  32. cb.Invoke(SaveFilePanel(title, directory, defaultName, extensions));
  33. }
  34. // EditorUtility.OpenFilePanelWithFilters extension filter format
  35. private static string[] GetFilterFromFileExtensionList(ExtensionFilter[] extensions) {
  36. var filters = new string[extensions.Length * 2];
  37. for (int i = 0; i < extensions.Length; i++) {
  38. filters[(i * 2)] = extensions[i].Name;
  39. filters[(i * 2) + 1] = string.Join(",", extensions[i].Extensions);
  40. }
  41. return filters;
  42. }
  43. }
  44. }
  45. #endif