StandaloneFilePicker.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. namespace Assets.HeroEditor4D.Common.Scripts.Common
  4. {
  5. public static class StandaloneFilePicker
  6. {
  7. #if UNITY_EDITOR
  8. public static IEnumerator OpenFile(string title, string directory, string extension, Action<bool, string, byte[]> callback)
  9. {
  10. var path = UnityEditor.EditorUtility.OpenFilePanel(title, directory, extension.Replace(".", null));
  11. if (!string.IsNullOrEmpty(path))
  12. {
  13. var bytes = System.IO.File.ReadAllBytes(path);
  14. callback(true, path, bytes);
  15. }
  16. else
  17. {
  18. callback(false, null, null);
  19. }
  20. yield break;
  21. }
  22. public static IEnumerator SaveFile(string title, string directory, string defaultName, string extension, byte[] bytes, Action<bool, string> callback)
  23. {
  24. var path = UnityEditor.EditorUtility.SaveFilePanel(title, directory, defaultName, extension.Replace(".", null));
  25. if (!string.IsNullOrEmpty(path))
  26. {
  27. System.IO.File.WriteAllBytes(path, bytes);
  28. callback(true, path);
  29. }
  30. else
  31. {
  32. callback(false, null);
  33. }
  34. yield break;
  35. }
  36. #elif UNITY_STANDALONE_WIN || UNITY_WSA
  37. public static IEnumerator OpenFile(string title, string directory, string extension, Action<bool, string, byte[]> callback)
  38. {
  39. throw new NotImplementedException("Import [Simple File Browser For Windows]: http://u3d.as/2QLg");
  40. //yield return SimpleFileBrowserForWindows.WindowsFileBrowser.OpenFile(title, directory, "File", new[] { extension }, callback);
  41. }
  42. public static IEnumerator SaveFile(string title, string directory, string defaultName, string extension, byte[] bytes, Action<bool, string> callback)
  43. {
  44. throw new NotImplementedException("Import [Simple File Browser For Windows]: http://u3d.as/2QLg");
  45. //yield return SimpleFileBrowserForWindows.WindowsFileBrowser.SaveFile(title, directory, defaultName, "Prefab", extension, bytes, callback);
  46. }
  47. #elif UNITY_WEBGL
  48. public static IEnumerator OpenFile(string title, string directory, string extension, Action<bool, string, byte[]> callback)
  49. {
  50. throw new NotImplementedException("Import [Simple File Browser for WebGL]: http://u3d.as/2W52");
  51. //SimpleFileBrowserForWebGL.WebFileBrowser.Upload((fileName, mime, bytes) => callback(true, null, bytes), extension);
  52. }
  53. public static IEnumerator SaveFile(string title, string directory, string defaultName, string extension, byte[] bytes, Action<bool, string> callback)
  54. {
  55. throw new NotImplementedException("Import [Simple File Browser for WebGL]: http://u3d.as/2W52");
  56. //SimpleFileBrowserForWebGL.WebFileBrowser.Download(defaultName, bytes);
  57. }
  58. #else
  59. public static IEnumerator OpenFile(string title, string directory, string extension, Action<bool, string, byte[]> callback)
  60. {
  61. throw new NotSupportedException();
  62. }
  63. public static IEnumerator SaveFile(string title, string directory, string defaultName, string extension, byte[] bytes, Action<bool, string> callback)
  64. {
  65. throw new NotSupportedException();
  66. }
  67. #endif
  68. }
  69. }