Editor の filedialog の platform 依存を隔離

This commit is contained in:
ousttrue
2024-12-09 16:27:33 +09:00
parent 15670d24b6
commit 267b080eac
7 changed files with 96 additions and 71 deletions

View File

@@ -6,18 +6,9 @@ namespace UniGLTF
{
public static class GltfImportMenu
{
public const string MENU_NAME = "Import glTF... (*.gltf|*.glb|*.zip)";
public static void ImportGltfFileToGameObject()
{
var path = EditorUtility.OpenFilePanel(MENU_NAME + ": open glb", "",
#if UNITY_EDITOR_OSX
// https://github.com/vrm-c/UniVRM/issues/1837
"glb"
#else
"gltf,glb,zip"
#endif
);
if (string.IsNullOrEmpty(path))
if(!UniGltfEditorDialog.TryOpenFilePanel("", out var path))
{
return;
}

View File

@@ -23,7 +23,7 @@ namespace UniGLTF
private static void ExportGameObjectToGltf() => GltfExportWindow.ExportGameObjectToGltfFile();
[MenuItem(UserGltfMenuPrefix + "/" + GltfImportMenu.MENU_NAME, priority = 2)]
[MenuItem(UserGltfMenuPrefix + "/" + UniGltfEditorDialog.IMPORT_MENU_NAME, priority = 2)]
private static void ImportGltfFile() => GltfImportMenu.ImportGltfFileToGameObject();

View File

@@ -156,8 +156,7 @@ namespace UniGLTF
if (GUILayout.Button("Export", GUILayout.MinWidth(100)))
{
var path = SaveFileDialog.GetPath(SaveTitle, SaveName, SaveExtensions);
if (!string.IsNullOrEmpty(path))
if (UniGltfEditorDialog.TrySaveFilePanel(SaveTitle, SaveName, SaveExtensions, out var path))
{
ExportPath(path);
// close

View File

@@ -1,55 +0,0 @@
using System.IO;
using UnityEditor;
using UnityEngine;
namespace UniGLTF
{
public static class SaveFileDialog
{
static string m_lastExportDir;
static string extensionString(string[] extensions)
{
#if UNITY_EDITOR_OSX
// in OSX multi extension cause exception.
// https://github.com/vrm-c/UniVRM/issues/1837
return extensions.Length > 0 ? extensions[0] : "";
#else
return string.Join(",", extensions);
#endif
}
public static string GetPath(string title, string name, params string[] extensions)
{
string directory = m_lastExportDir;
if (string.IsNullOrEmpty(directory))
{
directory = Directory.GetParent(Application.dataPath).ToString();
}
var path = EditorUtility.SaveFilePanel(title, directory, name, extensionString(extensions));
if (!string.IsNullOrEmpty(path))
{
m_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");
}
return path;
}
public static string GetDir(string title, string dir = null)
{
string directory = string.IsNullOrEmpty(dir) ? m_lastExportDir : dir;
if (string.IsNullOrEmpty(directory))
{
directory = Directory.GetParent(Application.dataPath).ToString();
}
var path = EditorUtility.SaveFolderPanel(title, directory, null);
if (!string.IsNullOrEmpty(path))
{
m_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");
}
return path;
}
}
}

View File

@@ -0,0 +1,91 @@
using System.IO;
using UnityEditor;
using UnityEngine;
namespace UniGLTF
{
public static class UniGltfEditorDialog
{
#if UNITY_EDITOR_WIN
// #if false
public const string IMPORT_MENU_NAME = "Import glTF... (*.gltf|*.glb|*.zip)";
static bool IsWindows()
{
return true;
}
#else
public const string IMPORT_MENU_NAME = "Import glTF... (*.glb)";
static bool IsWindows()
{
return false;
}
#endif
static string extensionString(string[] extensions)
{
if (IsWindows())
{
return string.Join(",", extensions);
}
else
{
// in OSX multi extension cause exception.
// https://github.com/vrm-c/UniVRM/issues/1837
// in Linux
// https://github.com/vrm-c/UniVRM/issues/2515
return extensions.Length > 0 ? extensions[0] : "";
}
}
public static bool TryOpenFilePanel(string directory, out string path)
{
path = EditorUtility.OpenFilePanel(IMPORT_MENU_NAME, directory,
// https://github.com/vrm-c/UniVRM/issues/1837
IsWindows() ? "gltf,glb,zip" : "glb"
);
if (string.IsNullOrEmpty(path))
{
return false;
}
return true;
}
static string s_lastExportDir;
public static bool TrySaveFilePanel(string title, string name, string[] extensions, out string path)
{
string directory = s_lastExportDir;
if (string.IsNullOrEmpty(directory))
{
directory = Directory.GetParent(Application.dataPath).ToString();
}
path = EditorUtility.SaveFilePanel(title, directory, name, extensionString(extensions));
if (string.IsNullOrEmpty(path))
{
return false;
}
s_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");
return true;
}
public static bool TryGetDir(string title, string dir, out string path)
{
string directory = string.IsNullOrEmpty(dir) ? s_lastExportDir : dir;
if (string.IsNullOrEmpty(directory))
{
directory = Directory.GetParent(Application.dataPath).ToString();
}
path = EditorUtility.SaveFolderPanel(title, directory, null);
if (string.IsNullOrEmpty(path))
{
return false;
}
s_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");
return true;
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 35dc28badc82c5e4a8aa813ddd3369fe
guid: 20581dc53a52e174cbaec605148d3e78
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -223,8 +223,7 @@ namespace UniVRM10
if (GUILayout.Button("Create new VRM10Object and default Expressions. select target folder"))
{
var saveName = GetSaveName(instance);
var dir = SaveFileDialog.GetDir(SaveTitle, System.IO.Path.GetDirectoryName(saveName));
if (!string.IsNullOrEmpty(dir))
if (UniGltfEditorDialog.TryGetDir(SaveTitle, System.IO.Path.GetDirectoryName(saveName), out var dir))
{
var expressions = new Dictionary<ExpressionPreset, VRM10Expression>();
foreach (ExpressionPreset expression in CachedEnum.GetValues<ExpressionPreset>())