using System; using Spine; using Spine.Unity; using UnityEngine; using AnimationState = Spine.AnimationState; namespace BlueWater.Players { public abstract class SpineController : MonoBehaviour { // Variables #region Variables // Components protected SkeletonAnimation SkeletonAnimation; protected AnimationState AnimationState; #endregion // Unity events #region Unity events private void OnDestroy() { if (AnimationState != null) { AnimationState.Start -= OnAnimationStart; AnimationState.Complete -= OnAnimationComplete; } } #endregion // Initialize methods #region Initialize methods public virtual void InitializeComponents(SkeletonAnimation skeletonAnimation) { if (skeletonAnimation == null) { Debug.LogError("SkeletonAnimation component is null."); return; } SkeletonAnimation = skeletonAnimation; AnimationState = SkeletonAnimation.AnimationState; if (AnimationState != null) { AnimationState.Complete += OnAnimationComplete; } else { Debug.LogError("AnimationState is not initialized."); } } #endregion // Methods #region Methods public void PlayAnimation(string animationName, bool isLoopActive, float speed = 1f) { if (SkeletonAnimation == null && AnimationState == null) return; if (string.IsNullOrEmpty(animationName)) { Debug.LogError($"{animationName}의 애니메이션은 존재하지 않습니다."); return; } AnimationState.TimeScale = speed; AnimationState.SetAnimation(0, animationName, isLoopActive); } public void SetSkin(string skinName) { if (SkeletonAnimation == null && AnimationState == null) return; if (string.IsNullOrEmpty(skinName)) { Debug.LogError($"{skinName}의 스킨 이름은 존재하지 않습니다."); return; } SkeletonAnimation.Skeleton.SetSkin(skinName); SkeletonAnimation.Skeleton.SetSlotsToSetupPose(); AnimationState.Apply(SkeletonAnimation.Skeleton); } protected abstract void OnAnimationStart(TrackEntry trackEntry); protected abstract void OnAnimationComplete(TrackEntry trackEntry); #endregion } }