yyl
2025-07-07 0b15db60b38265335e97f934fdcfe21f164504af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 
using Spine.Unity;
using UnityEngine;
using UnityEngine.EventSystems;
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;
 
    private UIEventTrigger uiEventTrigger;
 
    void Awake()
    {
        skeletonGraphic = gameObject.GetComponentInChildren<SkeletonGraphic>(true);
        uiEventTrigger = UIEventTrigger.Get(skeletonGraphic.gameObject);
 
        uiEventTrigger.OnClick = OnClick;
 
        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(GameObject go,PointerEventData eventData)
    {
        PlayAnimation(MotionName.attack);
    }
}