// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2026 Kybernetik //
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Animancer
{
/// Attribute for static methods which try to create a transition from an object.
///
/// The method signature must be:
/// static ITransition TryCreateTransition(Object target)
///
/// https://kybernetik.com.au/animancer/api/Animancer/TryCreateTransitionAttribute
[AttributeUsage(AttributeTargets.Method)]
public sealed class TryCreateTransitionAttribute : Attribute
{
/************************************************************************************************************************/
/// The base type of object which the attributed method can handle.
public Type ObjectType { get; private set; }
/************************************************************************************************************************/
/// Creates a new .
public TryCreateTransitionAttribute(Type objectType)
{
ObjectType = objectType;
}
/************************************************************************************************************************/
#if UNITY_EDITOR
/************************************************************************************************************************/
private static List> _Methods;
private static List _TargetTypes;
/// [Editor-Only] Ensures that all methods with this attribute have been gathered.
private static void InitializeMethods()
{
if (_Methods != null)
return;
_Methods = new();
_TargetTypes = new();
foreach (var method in TypeCache.GetMethodsWithAttribute())
{
try
{
var attributes = method.GetCustomAttributes(typeof(TryCreateTransitionAttribute), true);
if (!attributes.IsNullOrEmpty())
{
var attribute = attributes[0] as TryCreateTransitionAttribute;
if (attribute?.ObjectType != null)
_TargetTypes.Add(attribute.ObjectType);
}
var func = Delegate.CreateDelegate(typeof(Func