using System;
using System.Collections.Generic;
namespace TriLibCore.SFB
{
/// Represents a platform-specific file browser.
public class StandaloneFileBrowser
{
#if UNITY_EDITOR
private static IStandaloneFileBrowser _platformWrapper = new StandaloneFileBrowserEditor();
#else
#if UNITY_WSA
private static readonly IStandaloneFileBrowser _platformWrapper = new StandaloneFileBrowserWinRT();
#elif UNITY_ANDROID
private static readonly IStandaloneFileBrowser _platformWrapper = new StandaloneFileBrowserAndroid();
#elif UNITY_WEBGL
private static readonly IStandaloneFileBrowser _platformWrapper = new StandaloneFileBrowserWebGL();
#elif UNITY_STANDALONE_OSX
private static readonly IStandaloneFileBrowser _platformWrapper = new StandaloneFileBrowserMac();
#elif UNITY_IOS
private static readonly IStandaloneFileBrowser _platformWrapper = new StandaloneFileBrowserIOS();
#elif UNITY_STANDALONE_WIN
private static readonly IStandaloneFileBrowser _platformWrapper = new StandaloneFileBrowserWindows();
#elif UNITY_STANDALONE_LINUX
private static readonly IStandaloneFileBrowser _platformWrapper = new StandaloneFileBrowserLinux();
#else
private static readonly IStandaloneFileBrowser _platformWrapper = null;
#endif
#endif
///
/// Native open file dialog
///
/// Dialog title
/// Root directory
/// Allowed extension
/// Allow multiple file selection
/// Returns array of chosen items. Zero length array when cancelled
public static IList OpenFilePanel(string title, string directory, string extension, bool multiselect)
{
var extensions = string.IsNullOrEmpty(extension) ? null : new[] { new ExtensionFilter("", extension) };
return OpenFilePanel(title, directory, extensions, multiselect);
}
///
/// Native open file dialog
///
/// Dialog title
/// Root directory
/// List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")
/// Allow multiple file selection
/// Returns array of chosen items. Zero length array when cancelled
public static IList OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
{
return _platformWrapper.OpenFilePanel(title, directory, extensions, multiselect);
}
///
/// Native open file dialog async
///
/// Dialog title
/// Root directory
/// Allowed extension
/// Allow multiple file selection
/// Panel action callback
public static void OpenFilePanelAsync(string title, string directory, string extension, bool multiselect, Action> cb)
{
var extensions = string.IsNullOrEmpty(extension) ? null : new[] { new ExtensionFilter("", extension) };
OpenFilePanelAsync(title, directory, extensions, multiselect, cb);
}
///
/// Native open file dialog async
///
/// Dialog title
/// Root directory
/// List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")
/// Allow multiple file selection
/// Panel action callback
public static void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action> cb)
{
_platformWrapper.OpenFilePanelAsync(title, directory, extensions, multiselect, cb);
}
///
/// Native open folder dialog
///
///
/// Root directory
///
/// Returns array of chosen items. Zero length array when cancelled
public static IList OpenFolderPanel(string title, string directory, bool multiselect)
{
return _platformWrapper.OpenFolderPanel(title, directory, multiselect);
}
///
/// Native open folder dialog async
///
///
/// Root directory
///
/// Panel action callback
public static void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action> cb)
{
_platformWrapper.OpenFolderPanelAsync(title, directory, multiselect, cb);
}
///
/// Native save file dialog
///
/// Dialog title
/// Root directory
/// Default file name
/// File extension
/// Returns chosen item. Null when cancelled
public static ItemWithStream SaveFilePanel(string title, string directory, string defaultName, string extension)
{
var extensions = string.IsNullOrEmpty(extension) ? null : new[] { new ExtensionFilter("", extension) };
return SaveFilePanel(title, directory, defaultName, extensions);
}
///
/// Native save file dialog
///
/// Dialog title
/// Root directory
/// Default file name
/// List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")
/// Returns chosen item. Null when cancelled
public static ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
{
return _platformWrapper.SaveFilePanel(title, directory, defaultName, extensions);
}
///
/// Native save file dialog async
///
/// Dialog title
/// Root directory
/// Default file name
/// File extension
/// Panel action callback
/// Data to be saved (used on WebGL platform)
public static void SaveFilePanelAsync(string title, string directory, string defaultName, string extension, Action cb, byte[] data = null)
{
var extensions = string.IsNullOrEmpty(extension) ? null : new[] { new ExtensionFilter("", extension) };
SaveFilePanelAsync(title, directory, defaultName, extensions, cb, data);
}
///
/// Native save file dialog async
///
/// Dialog title
/// Root directory
/// Default file name
/// List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")
/// Panel action callback
/// Data to be saved (used on WebGL platform)
public static void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action cb, byte[] data = null)
{
#if UNITY_WEBGL && !UNITY_EDITOR
if (_platformWrapper is StandaloneFileBrowserWebGL standaloneFileBrowserWebGL)
{
standaloneFileBrowserWebGL.Data = data;
}
#endif
_platformWrapper.SaveFilePanelAsync(title, directory, defaultName, extensions, cb);
}
}
}