// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
#if UNITY_EDITOR
using System.Runtime.CompilerServices;
using UnityEngine;
namespace Animancer.Editor
{
/// [Editor-Only] Caches to reduce garbage allocations.
/// https://kybernetik.com.au/animancer/api/Animancer.Editor/AnimationEventCache
///
public static class AnimationEventCache
{
/************************************************************************************************************************/
private static readonly ConditionalWeakTable
ClipToEvents = new();
/************************************************************************************************************************/
///
/// Returns the and caches the result to avoid allocating more memory with
/// each subsequent call.
///
public static AnimationEvent[] GetCachedEvents(this AnimationClip clip)
{
if (!ClipToEvents.TryGetValue(clip, out var events))
{
events = clip.events;
ClipToEvents.Add(clip, events);
}
return events;
}
/************************************************************************************************************************/
/// Clears the cache.
public static void Clear()
=> ClipToEvents.Clear();
/************************************************************************************************************************/
/// Removes the `clip` from the cache so its events will be retrieved again next time.
public static void Remove(AnimationClip clip)
=> ClipToEvents.Remove(clip);
/************************************************************************************************************************/
}
}
#endif