ScriptedImporterEditorBase に Apply 前の状態を保持する。

This commit is contained in:
ousttrue
2021-06-21 15:01:06 +09:00
parent 21d0ce714e
commit e5b1f378c3
9 changed files with 111 additions and 32 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -12,7 +12,7 @@ namespace UniGLTF
{
public class RemapEditorAnimation : RemapEditorBase
{
public RemapEditorAnimation(IEnumerable<SubAssetKey> keys) : base(keys)
public RemapEditorAnimation(IEnumerable<SubAssetKey> keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter)
{ }
public void OnGUI(ScriptedImporter importer, GltfParser parser)

View File

@@ -8,24 +8,34 @@ using VRMShaders;
namespace UniGLTF
{
public delegate Dictionary<SubAssetKey, UnityEngine.Object> EditorMapGetterFunc();
public delegate void EditorMapSetterFunc(Dictionary<SubAssetKey, UnityEngine.Object> editorMap);
public abstract class RemapEditorBase
{
public static Dictionary<String, Type> s_typeMap = new Dictionary<string, Type>();
[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;
}
}
/// <summary>
@@ -35,9 +45,14 @@ namespace UniGLTF
/// </summary>
SubAssetKey[] m_keys;
protected RemapEditorBase(IEnumerable<SubAssetKey> keys)
EditorMapGetterFunc m_getter;
EditorMapSetterFunc m_setter;
protected RemapEditorBase(IEnumerable<SubAssetKey> keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter)
{
m_keys = keys.ToArray();
m_getter = getter;
m_setter = setter;
}
void RemapAndReload<T>(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<T>(Dictionary<ScriptedImporter.SourceAssetIdentifier, UnityEngine.Object> externalObjectMap) where T : UnityEngine.Object
protected void DrawRemapGUI<T>(
Dictionary<ScriptedImporter.SourceAssetIdentifier, UnityEngine.Object> 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();
}

View File

@@ -22,10 +22,13 @@ namespace UniGLTF
static bool s_foldMaterials = true;
static bool s_foldTextures = true;
public RemapEditorMaterial(IEnumerable<SubAssetKey> keys) : base(keys)
public RemapEditorMaterial(IEnumerable<SubAssetKey> keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter)
{ }
public void OnGUI(ScriptedImporter importer, GltfParser parser, ITextureDescriptorGenerator textureDescriptorGenerator, Func<string, string> textureDir, Func<string, string> materialDir)
public void OnGUI(ScriptedImporter importer, GltfParser parser,
ITextureDescriptorGenerator textureDescriptorGenerator,
Func<string, string> textureDir,
Func<string, string> materialDir)
{
var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D);
using (new EditorGUI.DisabledScope(hasExternal))

View File

@@ -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
{
/// <summary>
/// Apply されていない変更を保持する
/// </summary>
/// <typeparam name="SubAssetKey"></typeparam>
/// <typeparam name="UnityEngine.Object"></typeparam>
/// <returns></returns>
[SerializeField]
List<RemapEditorBase.SubAssetPair> m_editMap = new List<RemapEditorBase.SubAssetPair>();
protected Dictionary<SubAssetKey, UnityEngine.Object> GetEditorMap()
{
return m_editMap.ToDictionary(x => x.Key, x => x.Object);
}
protected void SetEditorMap(Dictionary<SubAssetKey, UnityEngine.Object> value)
{
Undo.RecordObject(this, "update editorMap");
m_editMap.Clear();
m_editMap.AddRange(value.Select(kv => new RemapEditorBase.SubAssetPair(kv.Key, kv.Value)));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3d0cae75bb03dd14fbe64fadd0eff64f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -16,8 +16,7 @@ namespace UniVRM10
{
public class RemapEditorVrm : RemapEditorBase
{
public RemapEditorVrm(
IEnumerable<SubAssetKey> keys) : base(keys)
public RemapEditorVrm(IEnumerable<SubAssetKey> keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter)
{ }
public void OnGUI(ScriptedImporter importer, GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm)

View File

@@ -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