using Spine.Unity; using UnityEngine; /// /// 翅膀预制体上的初始化脚本。 /// 负责管理翅膀的 UI 图标和 Spine 动画播放逻辑: /// 出现动画(appear)播放一次后自动切换到待机动画(idle)循环播放。 /// public class WingItemInit : MonoBehaviour { [Header("UI 图标")] [Tooltip("翅膀在列表/详情中显示的缩略图")] public Sprite iconWing; [Header("Spine 动画")] [Tooltip("Spine 动画组件,留空则自动获取")] public SkeletonAnimation skeletonAnimation; [Tooltip("出现动画名称(播放一次)")] public string appear="cx2"; [Tooltip("待机动画名称(循环播放)")] public string idle="idle"; private void Awake() { if (skeletonAnimation == null) skeletonAnimation = GetComponent(); } private void OnEnable() { PlayAppearThenIdle(); } /// /// 播放出现动画,播完后自动切换到待机循环。 /// 切换翅膀时也会调用此方法重新播放出现动画。 /// public void PlayAppearThenIdle() { if (skeletonAnimation == null) { skeletonAnimation = GetComponent(); if (skeletonAnimation == null) return; } var state = skeletonAnimation.AnimationState; if (state == null) return; // 清空当前轨道 state.ClearTrack(0); if (!string.IsNullOrEmpty(appear)) { // 播放出现动画(不循环) state.SetAnimation(0, appear, false); // 出现动画播完后自动切换到待机(循环) if (!string.IsNullOrEmpty(idle)) state.AddAnimation(0, idle, true, 0f); } else if (!string.IsNullOrEmpty(idle)) { // 没有出现动画,直接播待机 state.SetAnimation(0, idle, true); } } /// /// 重新播放出现动画(切换翅膀时调用)。 /// public void ReplayAppear() { PlayAppearThenIdle(); } }