diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs index e5facd50c..320814304 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs @@ -1,6 +1,7 @@ using UnityEditor; using UnityEngine; using System.IO; +using System.Linq; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; #else @@ -16,8 +17,8 @@ namespace UniGLTF GlbScriptedImporter m_importer; GltfParser m_parser; - RemapEditorMaterial m_materialEditor = new RemapEditorMaterial(); - RemapEditorAnimation m_animationEditor = new RemapEditorAnimation(); + RemapEditorMaterial m_materialEditor; + RemapEditorAnimation m_animationEditor; public override void OnEnable() { @@ -26,6 +27,13 @@ namespace UniGLTF m_importer = target as GlbScriptedImporter; m_parser = new GltfParser(); m_parser.ParsePath(m_importer.assetPath); + + var externalObjectMap = m_importer.GetExternalObjectMap(); + var materialGenerator = new GltfMaterialDescriptorGenerator(); + var materialKeys = m_parser.GLTF.materials.Select((_, i) => materialGenerator.Get(m_parser, i).SubAssetKey); + var textureKeys = new GltfTextureDescriptorGenerator(m_parser).Get().GetEnumerable().Select(x => x.SubAssetKey); + m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), externalObjectMap); + m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF), externalObjectMap); } enum Tabs diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs index 33cd5c599..5224d478b 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs @@ -1,6 +1,7 @@ using UnityEditor; using UnityEngine; using System.IO; +using System.Linq; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; #else @@ -16,8 +17,8 @@ namespace UniGLTF GltfScriptedImporter m_importer; GltfParser m_parser; - RemapEditorMaterial m_materialEditor = new RemapEditorMaterial(); - RemapEditorAnimation m_animationEditor = new RemapEditorAnimation(); + RemapEditorMaterial m_materialEditor; + RemapEditorAnimation m_animationEditor; public override void OnEnable() { @@ -26,6 +27,13 @@ namespace UniGLTF m_importer = target as GltfScriptedImporter; m_parser = new GltfParser(); m_parser.ParsePath(m_importer.assetPath); + + var externalObjectMap = m_importer.GetExternalObjectMap(); + var materialGenerator = new GltfMaterialDescriptorGenerator(); + var materialKeys = m_parser.GLTF.materials.Select((_, i) => materialGenerator.Get(m_parser, i).SubAssetKey); + var textureKeys = new GltfTextureDescriptorGenerator(m_parser).Get().GetEnumerable().Select(x => x.SubAssetKey); + m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), externalObjectMap); + m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF), externalObjectMap); } enum Tabs diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs index 167e433da..0a6808cdf 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs @@ -1,4 +1,6 @@ using System; +using System.Collections; +using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; @@ -8,8 +10,13 @@ using VRMShaders; namespace UniGLTF { - public class RemapEditorAnimation: RemapEditorBase + public class RemapEditorAnimation : RemapEditorBase { + public RemapEditorAnimation( + IEnumerable keys, + Dictionary externalObjectMap) : base(keys, externalObjectMap) + { } + public void OnGUI(ScriptedImporter importer, GltfParser parser) { var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is AnimationClip); @@ -21,7 +28,7 @@ namespace UniGLTF } } - DrawRemapGUI(importer, AnimationImporterUtil.EnumerateSubAssetKeys(parser.GLTF)); + DrawRemapGUI(); if (GUILayout.Button("Clear")) { @@ -58,6 +65,6 @@ namespace UniGLTF } AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); - } + } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs index ad7989cd5..f6179525e 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; +using System.Linq; using UnityEditor; using UnityEditor.Experimental.AssetImporters; using VRMShaders; @@ -7,6 +9,50 @@ namespace UniGLTF { public abstract class RemapEditorBase { + public struct SubAssetPair + { + public readonly SubAssetKey Key; + public readonly UnityEngine.Object Object; + + public SubAssetPair(SubAssetKey key, UnityEngine.Object o) + { + Key = key; + Object = o; + } + + public void Deconstruct(out SubAssetKey key, out UnityEngine.Object value) + { + key = Key; + value = Object; + } + } + + /// + /// Remap 対象は、このエディタのライフサイクル中に不変 + /// + /// apply, clear 時には ScriptedImporter は reimport され、新しい引数で new される + /// + /// + SubAssetPair[] m_keyValues; + + protected RemapEditorBase( + IEnumerable keys, + Dictionary externalObjectMap) + { + m_keyValues = keys.Select(x => + { + var id = new ScriptedImporter.SourceAssetIdentifier(x.Type, x.Name); + if (externalObjectMap.TryGetValue(id, out UnityEngine.Object value)) + { + return new SubAssetPair(x, value); + } + else + { + return new SubAssetPair(x, null); + } + }).ToArray(); + } + void RemapAndReload(ScriptedImporter self, UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object { self.AddRemap(sourceAssetIdentifier, obj); @@ -14,13 +60,17 @@ namespace UniGLTF AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); } - protected void DrawRemapGUI(ScriptedImporter importer, IEnumerable keys) where T : UnityEngine.Object + protected void DrawRemapGUI() where T : UnityEngine.Object { EditorGUI.indentLevel++; { - var map = importer.GetExternalObjectMap(); - foreach (var key in keys) + foreach (var (key, value) in m_keyValues) { + if (!typeof(T).IsAssignableFrom(key.Type)) + { + continue; + } + if (string.IsNullOrEmpty(key.Name)) { continue; @@ -28,12 +78,11 @@ namespace UniGLTF EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel(key.Name); - map.TryGetValue(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), out UnityEngine.Object value); var asset = EditorGUILayout.ObjectField(value, typeof(T), true) as T; if (asset != value) { // update - RemapAndReload(importer, new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), asset); + // RemapAndReload(importer, new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), asset); } EditorGUILayout.EndHorizontal(); } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs index 1c6e5134c..54192dcda 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs @@ -17,11 +17,16 @@ namespace UniGLTF /// /// Material, Texture の Remap, Extract /// - public class RemapEditorMaterial: RemapEditorBase + public class RemapEditorMaterial : RemapEditorBase { static bool s_foldMaterials = true; static bool s_foldTextures = true; + public RemapEditorMaterial( + IEnumerable keys, + Dictionary externalObjectMap) : base(keys, externalObjectMap) + { } + public void OnGUI(ScriptedImporter importer, GltfParser parser, ITextureDescriptorGenerator textureDescriptorGenerator, Func textureDir, Func materialDir) { var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D); @@ -39,13 +44,13 @@ namespace UniGLTF s_foldMaterials = EditorGUILayout.Foldout(s_foldMaterials, "Remapped Materials"); if (s_foldMaterials) { - DrawRemapGUI(importer, parser.GLTF.materials.Select(x => new SubAssetKey(typeof(Material), x.name))); + DrawRemapGUI(); } s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures"); if (s_foldTextures) { - DrawRemapGUI(importer, textureDescriptorGenerator.Get().GetEnumerable().Select(x => x.SubAssetKey)); + DrawRemapGUI(); } if (GUILayout.Button("Clear")) diff --git a/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs b/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs index 6819235f5..97fd573af 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs @@ -14,8 +14,13 @@ using UnityEditor.Experimental.AssetImporters; namespace UniVRM10 { - public class RemapEditorVrm: RemapEditorBase + public class RemapEditorVrm : RemapEditorBase { + public RemapEditorVrm( + IEnumerable keys, + Dictionary externalObjectMap) : base(keys, externalObjectMap) + { } + public void OnGUI(ScriptedImporter importer, GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm) { var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is VRM10Object || x.Value is VRM10Expression); @@ -28,10 +33,10 @@ namespace UniVRM10 } // meta - DrawRemapGUI(importer, new SubAssetKey[] { VRM10Object.SubAssetKey }); + DrawRemapGUI(); // expressions - DrawRemapGUI(importer, vrm.Expressions.Select(x => ExpressionKey.CreateFromVrm10(x).SubAssetKey)); + DrawRemapGUI(); if (GUILayout.Button("Clear")) { diff --git a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs index 558c1f7e6..ad496fed9 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs @@ -3,6 +3,8 @@ using UnityEngine; using UniGLTF; using System.IO; using UniGLTF.MeshUtility; +using System.Linq; +using VRMShaders; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; #else @@ -20,8 +22,8 @@ namespace UniVRM10 VrmLib.Model m_model; UniGLTF.Extensions.VRMC_vrm.VRMC_vrm m_vrm; - RemapEditorMaterial m_materialEditor = new RemapEditorMaterial(); - RemapEditorVrm m_vrmEditor = new RemapEditorVrm(); + RemapEditorMaterial m_materialEditor; + RemapEditorVrm m_vrmEditor; string m_message; @@ -40,6 +42,14 @@ namespace UniVRM10 m_vrm = result.Vrm; m_parser = result.Parser; m_model = ModelReader.Read(result.Parser); + + var externalObjectMap = m_importer.GetExternalObjectMap(); + var generator = new Vrm10MaterialDescriptorGenerator(); + var materialKeys = m_parser.GLTF.materials.Select((x, i) => generator.Get(m_parser, i).SubAssetKey); + var textureKeys = new GltfTextureDescriptorGenerator(m_parser).Get().GetEnumerable().Select(x => x.SubAssetKey); + m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), externalObjectMap); + var expressionSubAssetKeys = m_vrm.Expressions.Select(x => ExpressionKey.CreateFromVrm10(x).SubAssetKey); + m_vrmEditor = new RemapEditorVrm(new[] { VRM10Object.SubAssetKey }.Concat(expressionSubAssetKeys), externalObjectMap); } enum Tabs