RemapEditorBase

This commit is contained in:
ousttrue
2021-06-18 19:33:23 +09:00
parent 4f549c59d1
commit f6c0f475cd
12 changed files with 91 additions and 58 deletions

View File

@@ -16,6 +16,9 @@ namespace UniGLTF
GlbScriptedImporter m_importer;
GltfParser m_parser;
RemapEditorMaterial m_materialEditor = new RemapEditorMaterial();
RemapEditorAnimation m_animationEditor = new RemapEditorAnimation();
public override void OnEnable()
{
base.OnEnable();
@@ -45,11 +48,11 @@ namespace UniGLTF
break;
case Tabs.Animation:
EditorAnimation.OnGUIAnimation(m_importer, m_parser);
m_animationEditor.OnGUI(m_importer, m_parser);
break;
case Tabs.Materials:
EditorMaterial.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

@@ -16,6 +16,9 @@ namespace UniGLTF
GltfScriptedImporter m_importer;
GltfParser m_parser;
RemapEditorMaterial m_materialEditor = new RemapEditorMaterial();
RemapEditorAnimation m_animationEditor = new RemapEditorAnimation();
public override void OnEnable()
{
base.OnEnable();
@@ -45,11 +48,11 @@ namespace UniGLTF
break;
case Tabs.Animation:
EditorAnimation.OnGUIAnimation(m_importer, m_parser);
m_animationEditor.OnGUI(m_importer, m_parser);
break;
case Tabs.Materials:
EditorMaterial.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

@@ -8,9 +8,9 @@ using VRMShaders;
namespace UniGLTF
{
public static class EditorAnimation
public class RemapEditorAnimation: RemapEditorBase
{
public static void OnGUIAnimation(ScriptedImporter importer, GltfParser parser)
public void OnGUI(ScriptedImporter importer, GltfParser parser)
{
var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is AnimationClip);
using (new EditorGUI.DisabledScope(hasExternal))
@@ -21,7 +21,7 @@ namespace UniGLTF
}
}
importer.DrawRemapGUI<AnimationClip>(AnimationImporterUtil.EnumerateSubAssetKeys(parser.GLTF));
DrawRemapGUI<AnimationClip>(importer, AnimationImporterUtil.EnumerateSubAssetKeys(parser.GLTF));
if (GUILayout.Button("Clear"))
{
@@ -58,6 +58,6 @@ namespace UniGLTF
}
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
}
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: a15559f8a5ba86841a1ef6e08d37569a
guid: 11ced9da7136c3546bb9c65e3e35bb34
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,44 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using VRMShaders;
namespace UniGLTF
{
public abstract class RemapEditorBase
{
void RemapAndReload<T>(ScriptedImporter self, UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object
{
self.AddRemap(sourceAssetIdentifier, obj);
AssetDatabase.WriteImportSettingsIfDirty(self.assetPath);
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
}
protected void DrawRemapGUI<T>(ScriptedImporter importer, IEnumerable<SubAssetKey> keys) where T : UnityEngine.Object
{
EditorGUI.indentLevel++;
{
var map = importer.GetExternalObjectMap();
foreach (var key in keys)
{
if (string.IsNullOrEmpty(key.Name))
{
continue;
}
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);
}
EditorGUILayout.EndHorizontal();
}
}
EditorGUI.indentLevel--;
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 005298fbae4759b4896174bb6e129e9d
guid: 43b33bc54de8f3a4b82690bf090ed661
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -14,12 +14,15 @@ using UnityEditor.Experimental.AssetImporters;
namespace UniGLTF
{
public static class EditorMaterial
/// <summary>
/// Material, Texture の Remap, Extract
/// </summary>
public class RemapEditorMaterial: RemapEditorBase
{
static bool s_foldMaterials = true;
static bool s_foldTextures = true;
public static 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))
@@ -36,13 +39,13 @@ namespace UniGLTF
s_foldMaterials = EditorGUILayout.Foldout(s_foldMaterials, "Remapped Materials");
if (s_foldMaterials)
{
importer.DrawRemapGUI<UnityEngine.Material>(parser.GLTF.materials.Select(x => new SubAssetKey(typeof(Material), x.name)));
DrawRemapGUI<UnityEngine.Material>(importer, parser.GLTF.materials.Select(x => new SubAssetKey(typeof(Material), x.name)));
}
s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
if (s_foldTextures)
{
importer.DrawRemapGUI<UnityEngine.Texture>(textureDescriptorGenerator.Get().GetEnumerable().Select(x => x.SubAssetKey));
DrawRemapGUI<UnityEngine.Texture>(importer, textureDescriptorGenerator.Get().GetEnumerable().Select(x => x.SubAssetKey));
}
if (GUILayout.Button("Clear"))
@@ -53,14 +56,7 @@ namespace UniGLTF
}
}
public static void SetExternalUnityObject<T>(this ScriptedImporter self, UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object
{
self.AddRemap(sourceAssetIdentifier, obj);
AssetDatabase.WriteImportSettingsIfDirty(self.assetPath);
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
}
static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser, ITextureDescriptorGenerator textureDescriptorGenerator, Func<string, string> textureDir, Func<string, string> materialDir)
void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser, ITextureDescriptorGenerator textureDescriptorGenerator, Func<string, string> textureDir, Func<string, string> materialDir)
{
if (string.IsNullOrEmpty(self.assetPath))
{
@@ -74,7 +70,7 @@ namespace UniGLTF
Action<IEnumerable<UnityPath>> onCompleted = _ =>
{
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
self.ExtractMaterials(materialDir);
ExtractMaterials(self, materialDir);
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
};
@@ -90,7 +86,7 @@ namespace UniGLTF
);
}
public static void ExtractMaterials(this ScriptedImporter importer, Func<string, string> materialDir)
public void ExtractMaterials(ScriptedImporter importer, Func<string, string> materialDir)
{
if (string.IsNullOrEmpty(importer.assetPath))
{

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d87d4a592573eb048916575e202af37f
guid: 85720aed7aabf5e4c86c9ea492f05f00
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -46,33 +46,6 @@ namespace UniGLTF
.Select(x => (new SubAssetKey(typeof(T), x.name), x as T));
}
public static void DrawRemapGUI<T>(this ScriptedImporter importer, IEnumerable<SubAssetKey> keys) where T : UnityEngine.Object
{
EditorGUI.indentLevel++;
{
var map = importer.GetExternalObjectMap();
foreach (var key in keys)
{
if (string.IsNullOrEmpty(key.Name))
{
continue;
}
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
importer.SetExternalUnityObject(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), asset);
}
EditorGUILayout.EndHorizontal();
}
}
EditorGUI.indentLevel--;
}
/// <summary>
/// subAsset を 指定された path に extract する
/// </summary>

View File

@@ -14,9 +14,9 @@ using UnityEditor.Experimental.AssetImporters;
namespace UniVRM10
{
public static class EditorVrm
public class RemapEditorVrm: RemapEditorBase
{
public static void OnGUI(ScriptedImporter importer, GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm)
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);
using (new EditorGUI.DisabledScope(hasExternal))
@@ -28,10 +28,10 @@ namespace UniVRM10
}
// meta
importer.DrawRemapGUI<VRM10Object>(new SubAssetKey[] { VRM10Object.SubAssetKey });
DrawRemapGUI<VRM10Object>(importer, new SubAssetKey[] { VRM10Object.SubAssetKey });
// expressions
importer.DrawRemapGUI<VRM10Expression>(vrm.Expressions.Select(x => ExpressionKey.CreateFromVrm10(x).SubAssetKey));
DrawRemapGUI<VRM10Expression>(importer, vrm.Expressions.Select(x => ExpressionKey.CreateFromVrm10(x).SubAssetKey));
if (GUILayout.Button("Clear"))
{

View File

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

View File

@@ -20,6 +20,9 @@ namespace UniVRM10
VrmLib.Model m_model;
UniGLTF.Extensions.VRMC_vrm.VRMC_vrm m_vrm;
RemapEditorMaterial m_materialEditor = new RemapEditorMaterial();
RemapEditorVrm m_vrmEditor = new RemapEditorVrm();
string m_message;
public override void OnEnable()
@@ -66,7 +69,7 @@ namespace UniVRM10
case Tabs.Materials:
if (m_parser != null && m_vrm != null)
{
EditorMaterial.OnGUI(m_importer, m_parser, new Vrm10TextureDescriptorGenerator(m_parser),
m_materialEditor.OnGUI(m_importer, m_parser, new Vrm10TextureDescriptorGenerator(m_parser),
assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.vrm1.Textures",
assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.vrm1.Materials");
}
@@ -75,7 +78,7 @@ namespace UniVRM10
case Tabs.Vrm:
if (m_parser != null && m_vrm != null)
{
EditorVrm.OnGUI(m_importer, m_parser, m_vrm);
m_vrmEditor.OnGUI(m_importer, m_parser, m_vrm);
}
break;
}