diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs index 13ab4911c..b9dc9b448 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs @@ -2,6 +2,8 @@ using UnityEditor; using UnityEngine; using System.IO; using System.Linq; +using System.Collections.Generic; +using VRMShaders; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; #else @@ -12,7 +14,7 @@ using UnityEditor.Experimental.AssetImporters; namespace UniGLTF { [CustomEditor(typeof(GlbScriptedImporter))] - public class GlbScriptedImporterEditorGUI : ScriptedImporterEditor + public class GlbScriptedImporterEditorGUI : ScriptedImporterEditorBase { GlbScriptedImporter m_importer; GltfParser m_parser; @@ -31,8 +33,8 @@ namespace UniGLTF 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)); - m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF)); + m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), GetEditorMap, SetEditorMap); + m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF), GetEditorMap, SetEditorMap); } enum Tabs @@ -59,7 +61,8 @@ namespace UniGLTF break; case Tabs.Materials: - m_materialEditor.OnGUI(m_importer, m_parser, new GltfTextureDescriptorGenerator(m_parser), + m_materialEditor.OnGUI(m_importer, m_parser, + new GltfTextureDescriptorGenerator(m_parser), assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.Textures", assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.Materials"); break; diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs index 049446279..bb9be9f46 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs @@ -2,6 +2,8 @@ using UnityEditor; using UnityEngine; using System.IO; using System.Linq; +using System.Collections.Generic; +using VRMShaders; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; #else @@ -12,7 +14,7 @@ using UnityEditor.Experimental.AssetImporters; namespace UniGLTF { [CustomEditor(typeof(GltfScriptedImporter))] - public class GltfScriptedImporterEditorGUI : ScriptedImporterEditor + public class GltfScriptedImporterEditorGUI : ScriptedImporterEditorBase { GltfScriptedImporter m_importer; GltfParser m_parser; @@ -31,8 +33,8 @@ namespace UniGLTF 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)); - m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF)); + m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), GetEditorMap, SetEditorMap); + m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF), GetEditorMap, SetEditorMap); } enum Tabs @@ -59,7 +61,8 @@ namespace UniGLTF break; case Tabs.Materials: - m_materialEditor.OnGUI(m_importer, m_parser, new GltfTextureDescriptorGenerator(m_parser), + m_materialEditor.OnGUI(m_importer, m_parser, + new GltfTextureDescriptorGenerator(m_parser), assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.Textures", assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.Materials"); break; diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs index 8290656bf..8d5f15d71 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs @@ -12,7 +12,7 @@ namespace UniGLTF { public class RemapEditorAnimation : RemapEditorBase { - public RemapEditorAnimation(IEnumerable keys) : base(keys) + public RemapEditorAnimation(IEnumerable keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter) { } public void OnGUI(ScriptedImporter importer, GltfParser parser) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs index 511ea804e..2dcc6d49c 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs @@ -8,24 +8,34 @@ using VRMShaders; namespace UniGLTF { + public delegate Dictionary EditorMapGetterFunc(); + public delegate void EditorMapSetterFunc(Dictionary editorMap); + public abstract class RemapEditorBase { + public static Dictionary s_typeMap = new Dictionary(); + + [Serializable] public struct SubAssetPair { - public readonly SubAssetKey Key; - public readonly UnityEngine.Object Object; + [SerializeField] + public String Type; + + [SerializeField] + public String Name; + + public SubAssetKey Key => new SubAssetKey(s_typeMap[Type], Name); + + [SerializeField] + public UnityEngine.Object Object; public SubAssetPair(SubAssetKey key, UnityEngine.Object o) { - Key = key; + Type = key.Type.ToString(); + s_typeMap[Type] = key.Type; + Name = key.Name; Object = o; } - - public void Deconstruct(out SubAssetKey key, out UnityEngine.Object value) - { - key = Key; - value = Object; - } } /// @@ -35,9 +45,14 @@ namespace UniGLTF /// SubAssetKey[] m_keys; - protected RemapEditorBase(IEnumerable keys) + EditorMapGetterFunc m_getter; + EditorMapSetterFunc m_setter; + + protected RemapEditorBase(IEnumerable keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) { m_keys = keys.ToArray(); + m_getter = getter; + m_setter = setter; } void RemapAndReload(ScriptedImporter self, UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object @@ -47,7 +62,9 @@ namespace UniGLTF AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); } - protected void DrawRemapGUI(Dictionary externalObjectMap) where T : UnityEngine.Object + protected void DrawRemapGUI( + Dictionary externalObjectMap + ) where T : UnityEngine.Object { EditorGUI.indentLevel++; { @@ -65,12 +82,21 @@ namespace UniGLTF EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel(key.Name); - externalObjectMap.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) + + var editorMap = m_getter(); + if (editorMap.TryGetValue(key, out UnityEngine.Object value)) { - // update - // RemapAndReload(importer, new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), asset); + } + else + { + externalObjectMap.TryGetValue(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), out value); + } + + var newValue = EditorGUILayout.ObjectField(value, typeof(T), true) as T; + if (newValue != value) + { + editorMap[key] = newValue; + m_setter(editorMap); } EditorGUILayout.EndHorizontal(); } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs index 36b3c13ab..fecc9ee6d 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs @@ -22,10 +22,13 @@ namespace UniGLTF static bool s_foldMaterials = true; static bool s_foldTextures = true; - public RemapEditorMaterial(IEnumerable keys) : base(keys) + public RemapEditorMaterial(IEnumerable keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter) { } - public void OnGUI(ScriptedImporter importer, GltfParser parser, ITextureDescriptorGenerator textureDescriptorGenerator, Func textureDir, Func materialDir) + 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); using (new EditorGUI.DisabledScope(hasExternal)) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterEditorBase.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterEditorBase.cs new file mode 100644 index 000000000..b67839786 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterEditorBase.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEditor.Experimental.AssetImporters; +using UnityEngine; +using VRMShaders; + +namespace UniGLTF +{ + public abstract class ScriptedImporterEditorBase : ScriptedImporterEditor + { + /// + /// Apply されていない変更を保持する + /// + /// + /// + /// + [SerializeField] + List m_editMap = new List(); + + protected Dictionary GetEditorMap() + { + return m_editMap.ToDictionary(x => x.Key, x => x.Object); + } + + protected void SetEditorMap(Dictionary value) + { + Undo.RecordObject(this, "update editorMap"); + m_editMap.Clear(); + m_editMap.AddRange(value.Select(kv => new RemapEditorBase.SubAssetPair(kv.Key, kv.Value))); + } + } +} diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterEditorBase.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterEditorBase.cs.meta new file mode 100644 index 000000000..048be8338 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterEditorBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3d0cae75bb03dd14fbe64fadd0eff64f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs b/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs index 28a7e5d53..dbb60e263 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs @@ -16,8 +16,7 @@ namespace UniVRM10 { public class RemapEditorVrm : RemapEditorBase { - public RemapEditorVrm( - IEnumerable keys) : base(keys) + public RemapEditorVrm(IEnumerable keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter) { } public void OnGUI(ScriptedImporter importer, GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm) diff --git a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs index 01a51bbed..e2d77debb 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs @@ -5,6 +5,7 @@ using System.IO; using UniGLTF.MeshUtility; using System.Linq; using VRMShaders; +using System.Collections.Generic; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; #else @@ -15,7 +16,7 @@ using UnityEditor.Experimental.AssetImporters; namespace UniVRM10 { [CustomEditor(typeof(VrmScriptedImporter))] - public class VrmScriptedImporterEditorGUI : ScriptedImporterEditor + public class VrmScriptedImporterEditorGUI : ScriptedImporterEditorBase { VrmScriptedImporter m_importer; GltfParser m_parser; @@ -48,9 +49,9 @@ namespace UniVRM10 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)); + m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), GetEditorMap, SetEditorMap); var expressionSubAssetKeys = m_vrm.Expressions.Select(x => ExpressionKey.CreateFromVrm10(x).SubAssetKey); - m_vrmEditor = new RemapEditorVrm(new[] { VRM10Object.SubAssetKey }.Concat(expressionSubAssetKeys)); + m_vrmEditor = new RemapEditorVrm(new[] { VRM10Object.SubAssetKey }.Concat(expressionSubAssetKeys), GetEditorMap, SetEditorMap); } enum Tabs