hch
11 小时以前 2962c143fcde078b47a13684d02846a276a77be5
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
 
using System;
using Spine.Unity;
using UnityEngine;
using UnityEngine.UI;
 
public class HorseController : MonoBehaviour
{
    private GameObjectPoolManager.GameObjectPool pool;
    private int skinID;
    protected SkeletonGraphic skeletonGraphic;
 
    protected Spine.AnimationState spineAnimationState;
    private GameObject instanceGO;
 
    protected UIHeroController hero;
 
    private Action onComplete;
 
    private bool isHeroShowBefore = false;
 
    public void Create(int _skinID, int _heroSkinID = 0, float scale = 1f, Action _onComplete = null, string motionName = "idle")
    {
        pool = GameObjectPoolManager.Instance.GetPool(UILoader.LoadPrefab("UIHorse"));
        if (instanceGO == null)
        {
            instanceGO = pool.Request();
            instanceGO.transform.SetParent(transform);
            //transform 的Pivot Y是0,让instanceGO 居中
            instanceGO.transform.localPosition = new Vector3(0, instanceGO.GetComponent<RectTransform>().sizeDelta.y * 0.5f);
 
            //instanceGO.transform.localPosition = Vector3.zero;
            instanceGO.transform.localScale = Vector3.one;
            instanceGO.transform.localRotation = Quaternion.identity;
        }
        skeletonGraphic = instanceGO.transform.Find("Horse").GetComponent<SkeletonGraphic>();
 
        if (skinID == _skinID)
        {
            if (skinID == 0)
            {
                skeletonGraphic.enabled = false;
            }
            CreateHero(_heroSkinID, scale);
            //避免重复创建
            return;
        }
 
        skinID = _skinID;
        var skinConfig = HorseSkinConfig.Get(skinID);
 
        this.transform.localScale = Vector3.one * scale;
 
        onComplete = _onComplete;
 
        if (!transform.gameObject.activeSelf)
        {
            transform.SetActive(true);
        }
 
        
        if (skinConfig == null || string.IsNullOrEmpty(skinConfig.Spine))
        {
            //卸下坐骑的情况
            skeletonGraphic.enabled = false;
            spineAnimationState = null;
            CreateHero(_heroSkinID, scale);
            return;
        }
 
        skeletonGraphic.skeletonDataAsset = ResManager.Instance.LoadAsset<SkeletonDataAsset>("UIEffect/Spine/Horse", skinConfig.Spine);
        if (skeletonGraphic.skeletonDataAsset == null)
        {
 
            transform.SetActive(false);
            if (pool != null)
                pool.Release(instanceGO);
            skeletonGraphic = null;
            Destroy(instanceGO);
            Debug.LogError("未配置spine");
            return;
        }
        skeletonGraphic.enabled = true;
        skeletonGraphic.Initialize(true);
 
        skeletonGraphic.transform.localPosition = new Vector3(skinConfig.Poses[0], skinConfig.Poses[1], 0);
        isHeroShowBefore = skinConfig.heroFirst == 1;
        spineAnimationState = skeletonGraphic.AnimationState;
        spineAnimationState.Data.DefaultMix = 0f;
        if (motionName == "")
            motionName = GetFistSpineAnim();
        PlayAnimation(motionName, true);
        CreateHero(_heroSkinID, scale);
        spineAnimationState.Complete -= OnAnimationComplete;
        spineAnimationState.Complete += OnAnimationComplete;
    }
 
    public void CreateHero(int heroSkinID, float _scale)
    {
        if (instanceGO == null)
        {
            return;
        }
        // if (heroSkinID == 0)
        // {
        //     if (hero != null && hero.gameObject.activeSelf)
        //     {
        //         hero.SetActive(false);
        //     }
        //     return;    
        // }
        hero = instanceGO.GetComponentInChildren<UIHeroController>(true);
        if (hero == null)
        {
            return; 
        }
        if (heroSkinID == 0)
        {
            hero.SetActive(false);
            return;
        }
        hero.SetActive(true);
        hero.Create(heroSkinID, _scale);
        
        // 确保 BoneFollowerGraphic 引用到正确的坐骑 SkeletonGraphic
        var boneFollower = hero.GetComponent<BoneFollowerGraphic>();
        if (boneFollower != null && skeletonGraphic != null && skeletonGraphic.enabled)
        {
            boneFollower.enabled = true;
            // 设置 SkeletonGraphic 引用
            boneFollower.SkeletonGraphic = skeletonGraphic;
            // 约定骑乘点骨骼名
            boneFollower.boneName = "mountPoint";
            // 强制重新初始化 BoneFollowerGraphic 以应用新的设置
            boneFollower.Initialize();
        }
        else
        {
            boneFollower.enabled = false;
            hero.transform.localPosition = new Vector3(0, -50, 0);
        }
        
        if (isHeroShowBefore)
        {
            hero.transform.SetAsLastSibling();
        }
        else
        {
            hero.transform.SetAsFirstSibling();
        }
    }
 
    public void HeroPlay(string motionName, bool loop = false, bool replay=true)
    {
        if (hero == null)
            return;
        hero.PlayAnimation(motionName, loop, replay);
    }
 
 
    protected void OnDestroy()
    {
        if (spineAnimationState != null)
        {
            spineAnimationState.Complete -= OnAnimationComplete;
        }
        if (pool != null)
            pool.Release(instanceGO);
        skeletonGraphic = null;
        pool = null;
    }
 
    /// <summary>
    /// 播放 Spine 动画
    /// </summary>
    /// <param name="motionName">动作名</param>
    /// <param name="loop">循环</param>
    /// <param name="replay">如果相同动作是否再次重播,比如跑步重播就会跳帧不顺滑</param>
    public virtual void PlayAnimation(string motionName, bool loop = false, bool replay=true)
    {
        if (spineAnimationState == null) return;
 
        if (GetCurrentAnimationName() == motionName && !replay)
            return;
 
        // 直接使用 ToString() 而不是调用 GetAnimationName
        spineAnimationState.SetAnimation(0, motionName.ToString(), loop);
 
        HeroPlay(motionName, loop, replay);
    }
 
    // 播放第一个动画(作为默认动画)
    string GetFistSpineAnim()
    {
        var skeletonData = skeletonGraphic.Skeleton.Data;
        if (skeletonData.Animations.Count > 0)
        {
            return skeletonData.Animations.Items[0].Name;
        }
        else
        {
            Debug.LogError("Spine 数据中没有找到任何动画!武将皮肤:" + skinID);
        }
        return "";
    }
 
    /// <summary>
    /// 获取当前正在播放的 Spine 动画名称
    /// </summary>
    /// <returns>当前动画名称,如果没有动画则返回空字符串</returns>
    public string GetCurrentAnimationName()
    {
        if (spineAnimationState == null || spineAnimationState.GetCurrent(0) == null)
        {
            return string.Empty;
        }
        return spineAnimationState.GetCurrent(0).Animation.Name;
    }
 
 
 
    /// <summary>
    /// 动画完成事件处理
    /// </summary>
    protected virtual void OnAnimationComplete(Spine.TrackEntry trackEntry)
    {
        onComplete?.Invoke();
    }
 
    //越大越快
    public void SetSpeed(float speed)
    {
        spineAnimationState.TimeScale = speed;
    }
 
    public void SetEnabled(bool isEnable)
    { 
        if (skeletonGraphic == null)
        {
            return;
        }
        skeletonGraphic.enabled = isEnable;
    }
}