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