2022-01-30 12:03:37 +00:00
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using System.Runtime.InteropServices;
|
2024-01-27 21:44:17 +00:00
|
|
|
using Ookii.Dialogs.WinForms;
|
2022-01-30 12:03:37 +00:00
|
|
|
|
|
|
|
namespace SFB {
|
|
|
|
// For fullscreen support
|
|
|
|
// - WindowWrapper class and GetActiveWindow() are required for modal file dialog.
|
|
|
|
// - "PlayerSettings/Visible In Background" should be enabled, otherwise when file dialog opened app window minimizes automatically.
|
|
|
|
|
|
|
|
public class WindowWrapper : IWin32Window {
|
|
|
|
private IntPtr _hwnd;
|
|
|
|
public WindowWrapper(IntPtr handle) { _hwnd = handle; }
|
|
|
|
public IntPtr Handle { get { return _hwnd; } }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class StandaloneFileBrowserWindows : IStandaloneFileBrowser {
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
private static extern IntPtr GetActiveWindow();
|
|
|
|
|
|
|
|
public string[] OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect) {
|
|
|
|
var fd = new VistaOpenFileDialog();
|
|
|
|
fd.Title = title;
|
2024-01-27 21:44:17 +00:00
|
|
|
fd.InitialDirectory = directory;
|
|
|
|
fd.Multiselect = multiselect;
|
2022-01-30 12:03:37 +00:00
|
|
|
if (extensions != null) {
|
|
|
|
fd.Filter = GetFilterFromFileExtensionList(extensions);
|
|
|
|
fd.FilterIndex = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fd.Filter = string.Empty;
|
|
|
|
}
|
|
|
|
var res = fd.ShowDialog(new WindowWrapper(GetActiveWindow()));
|
|
|
|
var filenames = res == DialogResult.OK ? fd.FileNames : new string[0];
|
|
|
|
fd.Dispose();
|
|
|
|
return filenames;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<string[]> cb) {
|
|
|
|
cb.Invoke(OpenFilePanel(title, directory, extensions, multiselect));
|
|
|
|
}
|
|
|
|
|
|
|
|
public string[] OpenFolderPanel(string title, string directory, bool multiselect) {
|
|
|
|
var fd = new VistaFolderBrowserDialog();
|
|
|
|
fd.Description = title;
|
2024-01-27 21:44:17 +00:00
|
|
|
fd.RootFolder = Environment.SpecialFolder.MyComputer;
|
|
|
|
fd.SelectedPath = directory;
|
2022-01-30 12:03:37 +00:00
|
|
|
var res = fd.ShowDialog(new WindowWrapper(GetActiveWindow()));
|
2024-01-27 21:44:17 +00:00
|
|
|
var filenames = res == DialogResult.OK ? new[] { fd.SelectedPath } : new string[0];
|
2022-01-30 12:03:37 +00:00
|
|
|
fd.Dispose();
|
|
|
|
return filenames;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<string[]> cb) {
|
|
|
|
cb.Invoke(OpenFolderPanel(title, directory, multiselect));
|
|
|
|
}
|
|
|
|
|
|
|
|
public string SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions) {
|
|
|
|
var fd = new VistaSaveFileDialog();
|
|
|
|
fd.Title = title;
|
2024-01-27 21:44:17 +00:00
|
|
|
fd.InitialDirectory = directory;
|
|
|
|
fd.FileName = defaultName;
|
2022-01-30 12:03:37 +00:00
|
|
|
if (extensions != null) {
|
|
|
|
fd.Filter = GetFilterFromFileExtensionList(extensions);
|
|
|
|
fd.FilterIndex = 1;
|
|
|
|
fd.DefaultExt = extensions[0].Extensions[0];
|
|
|
|
fd.AddExtension = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fd.DefaultExt = string.Empty;
|
|
|
|
fd.Filter = string.Empty;
|
|
|
|
fd.AddExtension = false;
|
|
|
|
}
|
|
|
|
var res = fd.ShowDialog(new WindowWrapper(GetActiveWindow()));
|
|
|
|
var filename = res == DialogResult.OK ? fd.FileName : "";
|
|
|
|
fd.Dispose();
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<string> cb) {
|
|
|
|
cb.Invoke(SaveFilePanel(title, directory, defaultName, extensions));
|
|
|
|
}
|
|
|
|
|
|
|
|
// .NET Framework FileDialog Filter format
|
|
|
|
// https://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.filter
|
|
|
|
private static string GetFilterFromFileExtensionList(ExtensionFilter[] extensions) {
|
2024-01-27 21:44:17 +00:00
|
|
|
if (extensions == null) return "";
|
2022-01-30 12:03:37 +00:00
|
|
|
var filterString = "";
|
|
|
|
foreach (var filter in extensions) {
|
2024-01-27 21:44:17 +00:00
|
|
|
filterString += filter.Name + " (";
|
2022-01-30 12:03:37 +00:00
|
|
|
|
|
|
|
foreach (var ext in filter.Extensions) {
|
|
|
|
filterString += "*." + ext + ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
filterString = filterString.Remove(filterString.Length - 1);
|
|
|
|
filterString += ") |";
|
|
|
|
|
|
|
|
foreach (var ext in filter.Extensions) {
|
|
|
|
filterString += "*." + ext + "; ";
|
|
|
|
}
|
|
|
|
|
|
|
|
filterString += "|";
|
|
|
|
}
|
|
|
|
filterString = filterString.Remove(filterString.Length - 1);
|
|
|
|
return filterString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-27 21:44:17 +00:00
|
|
|
#endif
|