using UnityEngine; // 유니티 기능을 불러올거에요 -> UnityEngine을 using System.Collections.Generic; // 딕셔너리를 사용할거에요 -> System.Collections.Generic을 /// /// 애니메이션 클립 길이를 런타임에 캐싱하고 조회하는 유틸리티 /// public class AnimationLengthCache // 클래스를 선언할거에요 -> 애니메이션 길이 캐싱을 담당하는 AnimationLengthCache를 { private readonly Animator _animator; // 변수를 선언할거에요 -> 애니메이터 참조를 저장할 _animator를 private readonly Dictionary _clipLengthCache = new Dictionary(); // 변수를 선언할거에요 -> 클립 이름과 길이를 저장할 캐시를 public AnimationLengthCache(Animator animator) // 생성자를 만들거에요 -> 애니메이터를 받는 { _animator = animator; // 값을 저장할거에요 -> 전달받은 애니메이터를 CacheAllClipLengths(); // 함수를 실행할거에요 -> 모든 클립 길이를 미리 저장하는 } private void CacheAllClipLengths() // 함수를 선언할거에요 -> 초기 캐싱 로직을 { _clipLengthCache.Clear(); // 비울거에요 -> 기존 데이터를 if (_animator == null || _animator.runtimeAnimatorController == null) return; // 조건이 맞으면 중단할거에요 -> 애니메이터가 없으면 foreach (AnimationClip clip in _animator.runtimeAnimatorController.animationClips) // 반복할거에요 -> 모든 클립을 { if (!_clipLengthCache.ContainsKey(clip.name)) // 조건이 맞으면 실행할거에요 -> 캐시에 없는 이름이라면 _clipLengthCache[clip.name] = clip.length; // 값을 저장할거에요 -> 길이를 } } public float GetClipLength(string clipName, float fallback = 1.0f) // 함수를 선언할거에요 -> 길이를 조회하는 GetClipLength를 { if (_clipLengthCache.TryGetValue(clipName, out float length)) return length; // 조건이 맞으면 반환할거에요 -> 캐시에서 찾은 길이를 if (_animator != null) // 조건이 맞으면 실행할거에요 -> 애니메이터가 있다면 (비상용) { AnimatorStateInfo state = _animator.GetCurrentAnimatorStateInfo(0); // 정보를 가져올거에요 -> 현재 상태를 if (state.IsName(clipName)) return state.length; // 조건이 맞으면 반환할거에요 -> 현재 상태의 길이를 } return fallback; // 반환할거에요 -> 기본값을 (못 찾았을 때) } }