yyl
9 天以前 b1f98c42a6b859b35c26e0722efb38a6e9c215be
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
using System.Collections;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
 
public class TotalDamageDisplayer : MonoBehaviour
{
    public Image damageBackground;
    public Text textDamage;
    public Text textTotalDesc; //总伤害或者总治疗
 
    public UniTask task = default;
 
    private Coroutine hideCoroutine;
    private int hideVersion = 0;
 
    private long damage = 0;
 
    private long heal = 0;
 
    public void SetDamage(BattleDmgInfo dmgInfo)
    {
        // 先统一停止并清理此前的隐藏协程(如果有)
        ClearHideCoroutine();
 
        if (!gameObject.activeInHierarchy)
            gameObject.SetActive(true);
 
        if (dmgInfo == null)
            return;
 
        if (dmgInfo.IsType(DamageType.Recovery))
        {
            // 保持原有处理逻辑位置
            foreach (var h in dmgInfo.damageList)
            {
                heal += h;
            }
            textDamage.text = BattleUtility.DisplayDamageNum(heal, BattleConst.BattleTotalRecoverType);
            textTotalDesc.text = "总治疗";
        }
        else if (dmgInfo.IsType(DamageType.Damage) || dmgInfo.IsType(DamageType.Realdamage))
        {
            // 保持原有处理逻辑位置
            foreach (var d in dmgInfo.damageList)
            {
                damage += d;
            }
            textDamage.text = BattleUtility.DisplayDamageNum(damage, BattleConst.BattleTotalDamageType);
            textTotalDesc.text = "总伤害";
        }
 
        textDamage.transform.DOPunchScale(Vector3.one * 0.2f, 0.2f, 1).OnComplete(() =>
        {
            textDamage.transform.localScale = Vector3.one;
        });
 
        if (dmgInfo.isLastHit)
        {
            // 启动新的隐藏协程,先生成新的版本号以用于协程有效性校验
            hideVersion++;
            int myVersion = hideVersion;
 
            damage = 0;
            heal = 0;
 
            var battleField = BattleManager.Instance.GetBattleField(dmgInfo.battleFieldGuid);
 
            float ms = 500f / battleField.speedRatio;
 
            hideCoroutine = StartCoroutine(HideAfterDelayCoroutine(ms, myVersion));
 
            task = default;
        }
    }
 
    protected void OnDisable()
    {
        ClearHideCoroutine();
        hideVersion++;
    }
 
    protected void OnDestroy()
    {
        ClearHideCoroutine();
        hideVersion++;
    }
 
    public void CancelHide()
    {
        ClearHideCoroutine();
        hideVersion++;
    }
 
    private void ClearHideCoroutine()
    {
        if (hideCoroutine != null)
        {
            try { StopCoroutine(hideCoroutine); } catch { }
            hideCoroutine = null;
        }
    }
 
    private IEnumerator HideAfterDelayCoroutine(float secondsDelay, int version = 0)
    {
        yield return new WaitForSeconds(secondsDelay);
 
        if (version != 0 && version != hideVersion)
            yield break;
 
        if (this == null) yield break;
        if (gameObject != null)
            gameObject.SetActive(false);
 
        if (hideCoroutine != null)
            hideCoroutine = null;
    }
}