// MIT License - Copyright (c) 2025 wallstop // Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE // ReSharper disable once CheckNamespace namespace WallstopStudios.UnityHelpers.Core.Extension { using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif public static partial class UnityExtensions { #if UNITY_EDITOR /// /// Extracts all Sprite objects referenced in an AnimationClip. /// /// The AnimationClip to extract sprites from. /// An enumerable of all Sprite objects found in the animation clip. /// /// Thread Safety: Must be called from Unity main thread. Editor-only. /// Null Handling: Returns empty enumerable if clip is null. /// Performance: O(n*m) where n is number of bindings and m is keyframes per binding. /// Allocations: Allocates arrays for bindings and keyframes. /// Unity Behavior: Only available in Unity Editor. Uses AnimationUtility. /// Edge Cases: Only returns Sprite object references, ignores other object types. /// public static IEnumerable GetSpritesFromClip(this AnimationClip clip) { if (clip == null) { yield break; } foreach ( EditorCurveBinding binding in AnimationUtility.GetObjectReferenceCurveBindings(clip) ) { ObjectReferenceKeyframe[] keyframes = AnimationUtility.GetObjectReferenceCurve( clip, binding ); foreach (ObjectReferenceKeyframe frame in keyframes) { if (frame.value is Sprite sprite) { yield return sprite; } } } } #endif } }