Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature10/remap editor #1053

Merged
merged 6 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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
Expand All @@ -11,18 +14,27 @@
namespace UniGLTF
{
[CustomEditor(typeof(GlbScriptedImporter))]
public class GlbScriptedImporterEditorGUI : ScriptedImporterEditor
public class GlbScriptedImporterEditorGUI : RemapScriptedImporterEditorBase
{
GlbScriptedImporter m_importer;
GltfParser m_parser;

RemapEditorMaterial m_materialEditor;
RemapEditorAnimation m_animationEditor;

public override void OnEnable()
{
base.OnEnable();

m_importer = target as GlbScriptedImporter;
m_parser = new GltfParser();
m_parser.ParsePath(m_importer.assetPath);

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), GetEditorMap, SetEditorMap);
m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF), GetEditorMap, SetEditorMap);
}

enum Tabs
Expand All @@ -45,13 +57,16 @@ public override void OnInspectorGUI()
break;

case Tabs.Animation:
EditorAnimation.OnGUIAnimation(m_importer, m_parser);
m_animationEditor.OnGUI(m_importer, m_parser);
RevertApplyRemapGUI(m_importer);
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");
RevertApplyRemapGUI(m_importer);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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
Expand All @@ -11,18 +14,27 @@
namespace UniGLTF
{
[CustomEditor(typeof(GltfScriptedImporter))]
public class GltfScriptedImporterEditorGUI : ScriptedImporterEditor
public class GltfScriptedImporterEditorGUI : RemapScriptedImporterEditorBase
{
GltfScriptedImporter m_importer;
GltfParser m_parser;

RemapEditorMaterial m_materialEditor;
RemapEditorAnimation m_animationEditor;

public override void OnEnable()
{
base.OnEnable();

m_importer = target as GltfScriptedImporter;
m_parser = new GltfParser();
m_parser.ParsePath(m_importer.assetPath);

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), GetEditorMap, SetEditorMap);
m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF), GetEditorMap, SetEditorMap);
}

enum Tabs
Expand All @@ -45,13 +57,16 @@ public override void OnInspectorGUI()
break;

case Tabs.Animation:
EditorAnimation.OnGUIAnimation(m_importer, m_parser);
m_animationEditor.OnGUI(m_importer, m_parser);
RevertApplyRemapGUI(m_importer);
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");
RevertApplyRemapGUI(m_importer);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
Expand All @@ -8,9 +10,12 @@

namespace UniGLTF
{
public static class EditorAnimation
public class RemapEditorAnimation : RemapEditorBase
{
public static void OnGUIAnimation(ScriptedImporter importer, GltfParser parser)
public RemapEditorAnimation(IEnumerable<SubAssetKey> keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter)
{ }

public void OnGUI(ScriptedImporter importer, GltfParser parser)
{
var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is AnimationClip);
using (new EditorGUI.DisabledScope(hasExternal))
Expand All @@ -21,14 +26,7 @@ public static void OnGUIAnimation(ScriptedImporter importer, GltfParser parser)
}
}

importer.DrawRemapGUI<AnimationClip>(AnimationImporterUtil.EnumerateSubAssetKeys(parser.GLTF));

if (GUILayout.Button("Clear"))
{
importer.ClearExternalObjects(
typeof(UnityEngine.AnimationClip)
);
}
DrawRemapGUI<AnimationClip>(importer.GetExternalObjectMap());
}

static string GetAndCreateFolder(string assetPath, string suffix)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using UnityEngine;
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
{
[SerializeField]
public String Type;

[SerializeField]
public String Name;

public SubAssetKey Key => new SubAssetKey(s_typeMap[Type], Name);
public ScriptedImporter.SourceAssetIdentifier ID => new AssetImporter.SourceAssetIdentifier(s_typeMap[Type], Name);

[SerializeField]
public UnityEngine.Object Object;

public SubAssetPair(SubAssetKey key, UnityEngine.Object o)
{
Type = key.Type.ToString();
s_typeMap[Type] = key.Type;
Name = key.Name;
Object = o;
}
}

/// <summary>
/// Remap 対象は、このエディタのライフサイクル中に不変
///
/// ExternalObjectMap は都度変わりうるのに注意。
/// </summary>
SubAssetKey[] m_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;
}

protected void DrawRemapGUI<T>(
Dictionary<ScriptedImporter.SourceAssetIdentifier, UnityEngine.Object> externalObjectMap
) where T : UnityEngine.Object
{
EditorGUI.indentLevel++;
{
foreach (var key in m_keys)
{
if (!typeof(T).IsAssignableFrom(key.Type))
{
continue;
}

if (string.IsNullOrEmpty(key.Name))
{
continue;
}

EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel(key.Name);

var editorMap = m_getter();
if (editorMap.TryGetValue(key, out UnityEngine.Object value))
{
}
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();
}
}
EditorGUI.indentLevel--;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@

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 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)
{
var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D);
using (new EditorGUI.DisabledScope(hasExternal))
Expand All @@ -36,31 +45,17 @@ public static void OnGUI(ScriptedImporter importer, GltfParser parser, ITextureD
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.GetExternalObjectMap());
}

s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
if (s_foldTextures)
{
importer.DrawRemapGUI<UnityEngine.Texture>(textureDescriptorGenerator.Get().GetEnumerable().Select(x => x.SubAssetKey));
}

if (GUILayout.Button("Clear"))
{
importer.ClearExternalObjects(
typeof(UnityEngine.Material),
typeof(UnityEngine.Texture));
DrawRemapGUI<UnityEngine.Texture>(importer.GetExternalObjectMap());
}
}

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))
{
Expand All @@ -73,8 +68,11 @@ static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser
};
Action<IEnumerable<UnityPath>> onCompleted = _ =>
{
// texture extract 後に importer 発動
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
self.ExtractMaterials(materialDir);

ExtractMaterials(self, materialDir);
// material extract 後に importer 発動
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
};

Expand All @@ -90,7 +88,7 @@ static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser
);
}

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))
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading