StandaloneFileBrowser.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. namespace TriLibCore.SFB
  4. {
  5. /// <summary>Represents a platform-specific file browser.</summary>
  6. public class StandaloneFileBrowser
  7. {
  8. #if UNITY_EDITOR
  9. private static IStandaloneFileBrowser<ItemWithStream> _platformWrapper = new StandaloneFileBrowserEditor();
  10. #else
  11. #if UNITY_WSA
  12. private static readonly IStandaloneFileBrowser<ItemWithStream> _platformWrapper = new StandaloneFileBrowserWinRT();
  13. #elif UNITY_ANDROID
  14. private static readonly IStandaloneFileBrowser<ItemWithStream> _platformWrapper = new StandaloneFileBrowserAndroid();
  15. #elif UNITY_WEBGL
  16. private static readonly IStandaloneFileBrowser<ItemWithStream> _platformWrapper = new StandaloneFileBrowserWebGL();
  17. #elif UNITY_STANDALONE_OSX
  18. private static readonly IStandaloneFileBrowser<ItemWithStream> _platformWrapper = new StandaloneFileBrowserMac();
  19. #elif UNITY_IOS
  20. private static readonly IStandaloneFileBrowser<ItemWithStream> _platformWrapper = new StandaloneFileBrowserIOS();
  21. #elif UNITY_STANDALONE_WIN
  22. private static readonly IStandaloneFileBrowser<ItemWithStream> _platformWrapper = new StandaloneFileBrowserWindows();
  23. #elif UNITY_STANDALONE_LINUX
  24. private static readonly IStandaloneFileBrowser<ItemWithStream> _platformWrapper = new StandaloneFileBrowserLinux();
  25. #else
  26. private static readonly IStandaloneFileBrowser<ItemWithStream> _platformWrapper = null;
  27. #endif
  28. #endif
  29. /// <summary>
  30. /// Native open file dialog
  31. /// </summary>
  32. /// <param name="title">Dialog title</param>
  33. /// <param name="directory">Root directory</param>
  34. /// <param name="extension">Allowed extension</param>
  35. /// <param name="multiselect">Allow multiple file selection</param>
  36. /// <returns>Returns array of chosen items. Zero length array when cancelled</returns>
  37. public static IList<ItemWithStream> OpenFilePanel(string title, string directory, string extension, bool multiselect)
  38. {
  39. var extensions = string.IsNullOrEmpty(extension) ? null : new[] { new ExtensionFilter("", extension) };
  40. return OpenFilePanel(title, directory, extensions, multiselect);
  41. }
  42. /// <summary>
  43. /// Native open file dialog
  44. /// </summary>
  45. /// <param name="title">Dialog title</param>
  46. /// <param name="directory">Root directory</param>
  47. /// <param name="extensions">List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")</param>
  48. /// <param name="multiselect">Allow multiple file selection</param>
  49. /// <returns>Returns array of chosen items. Zero length array when cancelled</returns>
  50. public static IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
  51. {
  52. return _platformWrapper.OpenFilePanel(title, directory, extensions, multiselect);
  53. }
  54. /// <summary>
  55. /// Native open file dialog async
  56. /// </summary>
  57. /// <param name="title">Dialog title</param>
  58. /// <param name="directory">Root directory</param>
  59. /// <param name="extension">Allowed extension</param>
  60. /// <param name="multiselect">Allow multiple file selection</param>
  61. /// <param name="cb">Panel action callback</param>
  62. public static void OpenFilePanelAsync(string title, string directory, string extension, bool multiselect, Action<IList<ItemWithStream>> cb)
  63. {
  64. var extensions = string.IsNullOrEmpty(extension) ? null : new[] { new ExtensionFilter("", extension) };
  65. OpenFilePanelAsync(title, directory, extensions, multiselect, cb);
  66. }
  67. /// <summary>
  68. /// Native open file dialog async
  69. /// </summary>
  70. /// <param name="title">Dialog title</param>
  71. /// <param name="directory">Root directory</param>
  72. /// <param name="extensions">List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")</param>
  73. /// <param name="multiselect">Allow multiple file selection</param>
  74. /// <param name="cb">Panel action callback</param>
  75. public static void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
  76. {
  77. _platformWrapper.OpenFilePanelAsync(title, directory, extensions, multiselect, cb);
  78. }
  79. /// <summary>
  80. /// Native open folder dialog
  81. /// </summary>
  82. /// <param name="title"></param>
  83. /// <param name="directory">Root directory</param>
  84. /// <param name="multiselect"></param>
  85. /// <returns>Returns array of chosen items. Zero length array when cancelled</returns>
  86. public static IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
  87. {
  88. return _platformWrapper.OpenFolderPanel(title, directory, multiselect);
  89. }
  90. /// <summary>
  91. /// Native open folder dialog async
  92. /// </summary>
  93. /// <param name="title"></param>
  94. /// <param name="directory">Root directory</param>
  95. /// <param name="multiselect"></param>
  96. /// <param name="cb">Panel action callback</param>
  97. public static void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
  98. {
  99. _platformWrapper.OpenFolderPanelAsync(title, directory, multiselect, cb);
  100. }
  101. /// <summary>
  102. /// Native save file dialog
  103. /// </summary>
  104. /// <param name="title">Dialog title</param>
  105. /// <param name="directory">Root directory</param>
  106. /// <param name="defaultName">Default file name</param>
  107. /// <param name="extension">File extension</param>
  108. /// <returns>Returns chosen item. Null when cancelled</returns>
  109. public static ItemWithStream SaveFilePanel(string title, string directory, string defaultName, string extension)
  110. {
  111. var extensions = string.IsNullOrEmpty(extension) ? null : new[] { new ExtensionFilter("", extension) };
  112. return SaveFilePanel(title, directory, defaultName, extensions);
  113. }
  114. /// <summary>
  115. /// Native save file dialog
  116. /// </summary>
  117. /// <param name="title">Dialog title</param>
  118. /// <param name="directory">Root directory</param>
  119. /// <param name="defaultName">Default file name</param>
  120. /// <param name="extensions">List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")</param>
  121. /// <returns>Returns chosen item. Null when cancelled</returns>
  122. public static ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
  123. {
  124. return _platformWrapper.SaveFilePanel(title, directory, defaultName, extensions);
  125. }
  126. /// <summary>
  127. /// Native save file dialog async
  128. /// </summary>
  129. /// <param name="title">Dialog title</param>
  130. /// <param name="directory">Root directory</param>
  131. /// <param name="defaultName">Default file name</param>
  132. /// <param name="extension">File extension</param>
  133. /// <param name="cb">Panel action callback</param>
  134. /// <param name="data">Data to be saved (used on WebGL platform)</param>
  135. public static void SaveFilePanelAsync(string title, string directory, string defaultName, string extension, Action<ItemWithStream> cb, byte[] data = null)
  136. {
  137. var extensions = string.IsNullOrEmpty(extension) ? null : new[] { new ExtensionFilter("", extension) };
  138. SaveFilePanelAsync(title, directory, defaultName, extensions, cb, data);
  139. }
  140. /// <summary>
  141. /// Native save file dialog async
  142. /// </summary>
  143. /// <param name="title">Dialog title</param>
  144. /// <param name="directory">Root directory</param>
  145. /// <param name="defaultName">Default file name</param>
  146. /// <param name="extensions">List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")</param>
  147. /// <param name="cb">Panel action callback</param>
  148. /// <param name="data">Data to be saved (used on WebGL platform)</param>
  149. public static void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb, byte[] data = null)
  150. {
  151. #if UNITY_WEBGL && !UNITY_EDITOR
  152. if (_platformWrapper is StandaloneFileBrowserWebGL standaloneFileBrowserWebGL)
  153. {
  154. standaloneFileBrowserWebGL.Data = data;
  155. }
  156. #endif
  157. _platformWrapper.SaveFilePanelAsync(title, directory, defaultName, extensions, cb);
  158. }
  159. }
  160. }