From 267b080eace116892e2ac0bcadda749d1731384c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 9 Dec 2024 16:27:33 +0900 Subject: [PATCH] =?UTF-8?q?Editor=20=E3=81=AE=20filedialog=20=E3=81=AE=20p?= =?UTF-8?q?latform=20=E4=BE=9D=E5=AD=98=E3=82=92=E9=9A=94=E9=9B=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Editor/GltfImportMenu.cs | 11 +-- Assets/UniGLTF/Editor/TopMenu.cs | 2 +- .../UniGLTF/ExportDialog/ExportDialogBase.cs | 3 +- .../UniGLTF/ExportDialog/SaveFileDialog.cs | 55 ----------- Assets/UniGLTF/Editor/UniGltfEditorDialog.cs | 91 +++++++++++++++++++ ...og.cs.meta => UniGltfEditorDialog.cs.meta} | 2 +- Assets/VRM10/Editor/Vrm10InstanceEditor.cs | 3 +- 7 files changed, 96 insertions(+), 71 deletions(-) delete mode 100644 Assets/UniGLTF/Editor/UniGLTF/ExportDialog/SaveFileDialog.cs create mode 100644 Assets/UniGLTF/Editor/UniGltfEditorDialog.cs rename Assets/UniGLTF/Editor/{UniGLTF/ExportDialog/SaveFileDialog.cs.meta => UniGltfEditorDialog.cs.meta} (83%) diff --git a/Assets/UniGLTF/Editor/GltfImportMenu.cs b/Assets/UniGLTF/Editor/GltfImportMenu.cs index 913dd69dc..cbc19bdfc 100644 --- a/Assets/UniGLTF/Editor/GltfImportMenu.cs +++ b/Assets/UniGLTF/Editor/GltfImportMenu.cs @@ -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; } diff --git a/Assets/UniGLTF/Editor/TopMenu.cs b/Assets/UniGLTF/Editor/TopMenu.cs index f5e4e9cdf..0d6e4f678 100644 --- a/Assets/UniGLTF/Editor/TopMenu.cs +++ b/Assets/UniGLTF/Editor/TopMenu.cs @@ -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(); diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExportDialogBase.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExportDialogBase.cs index eb67943ea..41631f61a 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExportDialogBase.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/ExportDialogBase.cs @@ -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 diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/SaveFileDialog.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/SaveFileDialog.cs deleted file mode 100644 index 9ac7cea24..000000000 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/SaveFileDialog.cs +++ /dev/null @@ -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; - } - } -} diff --git a/Assets/UniGLTF/Editor/UniGltfEditorDialog.cs b/Assets/UniGLTF/Editor/UniGltfEditorDialog.cs new file mode 100644 index 000000000..8976ffbad --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGltfEditorDialog.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/SaveFileDialog.cs.meta b/Assets/UniGLTF/Editor/UniGltfEditorDialog.cs.meta similarity index 83% rename from Assets/UniGLTF/Editor/UniGLTF/ExportDialog/SaveFileDialog.cs.meta rename to Assets/UniGLTF/Editor/UniGltfEditorDialog.cs.meta index 1135ac002..e8b6cdfeb 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/SaveFileDialog.cs.meta +++ b/Assets/UniGLTF/Editor/UniGltfEditorDialog.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 35dc28badc82c5e4a8aa813ddd3369fe +guid: 20581dc53a52e174cbaec605148d3e78 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM10/Editor/Vrm10InstanceEditor.cs b/Assets/VRM10/Editor/Vrm10InstanceEditor.cs index 7166bd9d5..060184fce 100644 --- a/Assets/VRM10/Editor/Vrm10InstanceEditor.cs +++ b/Assets/VRM10/Editor/Vrm10InstanceEditor.cs @@ -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(); foreach (ExpressionPreset expression in CachedEnum.GetValues())