Skip to content
Open
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
45 changes: 45 additions & 0 deletions Editor/AssetEditors/EditorHeaderHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Object = UnityEngine.Object;

[InitializeOnLoad]
static class EditorHeaderHook
{
private static Dictionary<Editor, (Object[] targets, MetadataEditor editor)> _targetsCache = new();

static EditorHeaderHook()
{
Editor.finishedDefaultHeaderGUI += DisplayMetadata;
}

static void DisplayMetadata(Editor editor)
{
if (!EditorUtility.IsPersistent(editor.target))
return;
if (!GUI.enabled)
return;

if (_targetsCache.TryGetValue(editor, out var cache) == false || Equality(cache.targets, editor.targets) == false)
{
cache.editor?.Dispose();
_targetsCache[editor] = cache = (editor.targets, new MetadataEditor(editor.targets));
}

cache.editor.OnInspectorGUI();

static bool Equality<T>(T[] a, T[] b)
{
if (a.Length != b.Length)
return false;

for (int i = 0; i < a.Length; i++)
{
if (ReferenceEquals(a[i], b[i]) == false)
return false;
}

return true;
}
}
}
17 changes: 0 additions & 17 deletions Editor/AssetEditors/MaterialMetadataEditor.cs

This file was deleted.

22 changes: 17 additions & 5 deletions Editor/Common/AssetMetadataUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,30 @@ public static void GetAll(UnityEngine.Object target, List<CustomAssetMetadata> m
if (metadata == null) throw new NullReferenceException(nameof(metadata));
EnsureInitialized();
var assetPath = AssetDatabase.GetAssetPath(target);
if (assetPath == null ||
string.IsNullOrEmpty(assetPath))
if (assetPath == null || string.IsNullOrEmpty(assetPath))
return;

var assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
foreach (var asset in assets)
if (target is SceneAsset)
{
if (asset is CustomAssetMetadata additionalDataAsset)
// calling LoadAllAssetsAtPath with a scene throws, this doesn't.
// Still, right now scenes do not support adding metadata so this cannot be validated further
foreach (var allMetadataType in AllMetadataTypes)
{
var additionalDataAsset = (CustomAssetMetadata)AssetDatabase.LoadAssetAtPath(assetPath, allMetadataType);
metadata.Add(additionalDataAsset);
}
}
else
{
var assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
foreach (var asset in assets)
{
if (asset is CustomAssetMetadata additionalDataAsset)
{
metadata.Add(additionalDataAsset);
}
}
}
}

public static CustomAssetMetadata Add(UnityEngine.Object target, Type type)
Expand Down
38 changes: 18 additions & 20 deletions Editor/Common/MetadataEditorInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,8 @@ static MetadataEditorInstance[] CreateEditors(UnityEngine.Object[] targets)
return metadataEditors;
}

static GUILayoutOption[] addMetadataButtonOptions = {
GUILayout.Width(230), GUILayout.Height(24),
GUILayout.ExpandWidth(false)
};

private static GUIContent addMetadataButton = new GUIContent("+", "Add Metadata");

public void OnInspectorGUI()
{
using (new EditorGUI.DisabledScope(!canOpenForEdit))
Expand All @@ -283,18 +280,7 @@ public void OnInspectorGUI()
// TODO: make it possible to re-order metadata like Components on GameObjects

GUILayout.BeginVertical();
EditorGUILayout.Space();
// We seem to get the metadata in reverse order from the assetdatabase, compared to the order we add them
// So to make things feel more consistent, we reverse the order
for (int i = metadataEditors.Length - 1; i >= 0; i--)
{
metadataEditors[i].OnInspectorGUI();
}

EditorGUILayout.Space();

GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
List<Type> filteredMetadata = ListPool<Type>.Get();
try
{
Expand All @@ -311,7 +297,11 @@ public void OnInspectorGUI()
}
using (new EditorGUI.DisabledScope(!canAddMetadata))
{
if (GUILayout.Button("Add Metadata", addMetadataButtonOptions))
// Embed the button inside the previous rect
var rect = GUILayoutUtility.GetRect(0,0,0,0);
rect.width = rect.height = EditorGUIUtility.singleLineHeight;
rect.y -= rect.height;
if (GUI.Button(rect, addMetadataButton))
{
// TODO: have a nicer dropdownmenu, more like the "add components" menu
var menu = new GenericMenu();
Expand All @@ -336,10 +326,18 @@ public void OnInspectorGUI()
{
ListPool<Type>.Release(filteredMetadata);
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();

GUILayout.EndVertical();
if (metadataEditors.Length > 0)
{
// We seem to get the metadata in reverse order from the assetdatabase, compared to the order we add them
// So to make things feel more consistent, we reverse the order
for (int i = metadataEditors.Length - 1; i >= 0; i--)
{
metadataEditors[i].OnInspectorGUI();
}
}

GUILayout.EndVertical();
}
}
}