StandaloneFileBrowser.cs 8.9 KB

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