using System;
namespace SFB {
public struct ExtensionFilter {
public string Name;
public string[] Extensions;
public ExtensionFilter(string filterName, params string[] filterExtensions) {
Name = filterName;
Extensions = filterExtensions;
}
}
public class StandaloneFileBrowser {
private static IStandaloneFileBrowser _platformWrapper = null;
static StandaloneFileBrowser() {
#if UNITY_STANDALONE_OSX
_platformWrapper = new StandaloneFileBrowserMac();
#elif UNITY_STANDALONE_WIN
_platformWrapper = new StandaloneFileBrowserWindows();
#elif UNITY_STANDALONE_LINUX
_platformWrapper = new StandaloneFileBrowserLinux();
#elif UNITY_EDITOR
_platformWrapper = new StandaloneFileBrowserEditor();
#endif
}
///
/// Native open file dialog
///
/// Dialog title
/// Root directory
/// Allowed extension
/// Allow multiple file selection
/// Returns array of chosen paths. Zero length array when cancelled
public static string[] 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 paths. Zero length array when cancelled
public static string[] 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
/// 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
/// 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
/// NOTE: Multiple folder selection doesn't supported on Windows
///
///
/// Root directory
///
/// Returns array of chosen paths. Zero length array when cancelled
public static string[] OpenFolderPanel(string title, string directory, bool multiselect) {
return _platformWrapper.OpenFolderPanel(title, directory, multiselect);
}
///
/// Native open folder dialog async
/// NOTE: Multiple folder selection doesn't supported on Windows
///
///
/// Root directory
///
/// 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 path. Empty string when cancelled
public static string 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 path. Empty string when cancelled
public static string 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
/// Callback")
public static void SaveFilePanelAsync(string title, string directory, string defaultName , string extension, Action cb) {
var extensions = string.IsNullOrEmpty(extension) ? null : new [] { new ExtensionFilter("", extension) };
SaveFilePanelAsync(title, directory, defaultName, extensions, cb);
}
///
/// Native save file dialog async
///
/// Dialog title
/// Root directory
/// Default file name
/// List of extension filters. Filter Example: new ExtensionFilter("Image Files", "jpg", "png")
/// Callback")
public static void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action cb) {
_platformWrapper.SaveFilePanelAsync(title, directory, defaultName, extensions, cb);
}
}
}