// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
#if UNITY_EDITOR
//#define LOG_CUSTOM_GUI_FACTORY
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;
namespace Animancer.Editor
{
/// [Editor-Only] Draws a custom GUI for an object.
///
/// Every non-abstract type implementing this interface must have at least one .
///
/// https://kybernetik.com.au/animancer/api/Animancer.Editor/CustomGUIFactory
///
public static class CustomGUIFactory
{
/************************************************************************************************************************/
private static readonly Dictionary
TargetTypeToGUIType = new();
static CustomGUIFactory()
{
foreach (var guiType in TypeCache.GetTypesWithAttribute(typeof(CustomGUIAttribute)))
{
if (guiType.IsAbstract ||
guiType.IsInterface)
continue;
if (!typeof(ICustomGUI).IsAssignableFrom(guiType))
{
Debug.LogWarning(
$"{guiType.FullName} has a {nameof(CustomGUIAttribute)}" +
$" but doesn't implement {nameof(ICustomGUI)}.");
continue;
}
var attribute = guiType.GetCustomAttribute();
if (attribute.TargetType != null)
{
TargetTypeToGUIType.Add(attribute.TargetType, guiType);
}
}
}
/************************************************************************************************************************/
private static readonly ConditionalWeakTable