New file |
| | |
| | | |
| | | using Spine.Unity; |
| | | using UnityEngine; |
| | | |
| | | public class UIHeroController : MonoBehaviour |
| | | { |
| | | public static UIHeroController Create(HeroSkinConfig skinCfg, Transform _parent = null) |
| | | { |
| | | // GameObject battleGO = ResManager.Instance.LoadAsset<GameObject>("Hero/SpineRes", skinCfg == null ? "Hero_001" : skinCfg.SpineRes); |
| | | GameObject battleGO = ResManager.Instance.LoadAsset<GameObject>("Hero/SpineRes", "Hero_001"); |
| | | GameObject instanceGO = null; |
| | | if (_parent != null) |
| | | { |
| | | instanceGO = GameObject.Instantiate(battleGO, _parent); |
| | | } |
| | | else |
| | | { |
| | | instanceGO = GameObject.Instantiate(battleGO); |
| | | } |
| | | UIHeroController heroController = instanceGO.AddMissingComponent<UIHeroController>(); |
| | | return heroController; |
| | | } |
| | | |
| | | protected SkeletonGraphic skeletonGraphic; |
| | | |
| | | protected Spine.AnimationState spineAnimationState; |
| | | |
| | | private Spine.TrackEntry currentTrackEntry; |
| | | |
| | | void Awake() |
| | | { |
| | | skeletonGraphic = gameObject.GetComponentInChildren<SkeletonGraphic>(true); |
| | | spineAnimationState = skeletonGraphic.AnimationState; |
| | | PlayAnimation(MotionName.idle, true); |
| | | |
| | | spineAnimationState.Complete += OnAnimationComplete; |
| | | } |
| | | |
| | | void Destroy() |
| | | { |
| | | spineAnimationState.Complete -= OnAnimationComplete; |
| | | } |
| | | |
| | | |
| | | public virtual Spine.TrackEntry PlayAnimation(MotionName motionName, bool loop = false) |
| | | { |
| | | if (spineAnimationState == null) return null; |
| | | |
| | | // 直接使用 ToString() 而不是调用 GetAnimationName |
| | | currentTrackEntry = spineAnimationState.SetAnimation(0, motionName.ToString(), loop); |
| | | return currentTrackEntry; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 动画完成事件处理 |
| | | /// </summary> |
| | | protected virtual void OnAnimationComplete(Spine.TrackEntry trackEntry) |
| | | { |
| | | // string animation = trackEntry.Animation.Name; |
| | | PlayAnimation(MotionName.idle, true); |
| | | } |
| | | |
| | | protected void OnClick() |
| | | { |
| | | PlayAnimation(MotionName.attack); |
| | | } |
| | | } |