yyl
6 天以前 f1e6527fd1a9281e254c000e21afb96c2da92e1c
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
using UnityEngine;
using System;
using UnityEngine.UI;
 
public class BattleTips : MonoBehaviour
{
    public Vector2 beginPos = Vector2.zero;
    public Vector2 endPos = new Vector2(0, 150);
 
    public float scaleChangeTime = 0.2667f; // 7~8帧 (8/30=0.2667秒)
    public float totalShowTime = 0.8f; // 总时间约24帧 (8+16=24帧)
    public float timer = 0f;
 
    public Vector3 beginScale = new Vector3(4f, 4f, 4f);
    public Vector3 endScale = new Vector3(2f, 2f, 2f);
    public Color beginColor = new Color(1f, 1f, 1f, 0.5f);
    public Color endColor = new Color(1f, 1f, 1f, 1f);
 
    public RectTransform rectTransform;
 
    public Text tipText;
 
    public Text artText;
 
    public Action OnFinish;
 
    private float speedRatio = 1f;
 
    public void SetSpeedRatio(float ratio)
    {
        speedRatio = ratio;
    }
 
    public void SetText(string text, bool useArtText = false)
    {
        //  初始放大200% 透明度50% 7~8帧内缩回100% 透明度到100% 再往上飘14~16帧 然后消失 (30帧/秒)
 
        // 8+16/30=0.8秒
        
 
        rectTransform.anchoredPosition = beginPos;
        rectTransform.localScale = beginScale;
 
        timer = 0f;
        gameObject.SetActive(true);
 
        if (useArtText)
        {
            artText.text = text;
            artText.color = beginColor;
            tipText.gameObject.SetActive(false);
            artText.gameObject.SetActive(true);
        }
        else
        {
            tipText.text = text;
            tipText.color = beginColor;
            artText.gameObject.SetActive(false);
            tipText.gameObject.SetActive(true);
        }
    }
 
 
    //  不要使用update
    public void Run()
    {
        if (!gameObject.activeSelf)
            return;
 
        if (timer >= totalShowTime)
        {
            gameObject.SetActive(false);
            OnFinish?.Invoke();
            OnFinish = null;
            return;
        }
 
        // 整个过程都往上飘
        float moveProgress = timer / totalShowTime;
        rectTransform.anchoredPosition = Vector2.Lerp(beginPos, endPos, moveProgress);
 
        // 阶段1: 7~8帧内缩放从beginScale到endScale,透明度从50%到100%
        if (timer < scaleChangeTime)
        {
            float scaleProgress = timer / scaleChangeTime;
            rectTransform.localScale = Vector3.Lerp(beginScale, endScale, scaleProgress);
 
            Color currentColor = Color.Lerp(beginColor, endColor, scaleProgress);
            if (tipText.gameObject.activeSelf)
                tipText.color = currentColor;
            if (artText.gameObject.activeSelf)
                artText.color = currentColor;
        }
        // 阶段2: 缩放完成后,保持endScale和100%透明度,继续往上飘
        else
        {
            rectTransform.localScale = endScale;
            
            if (tipText.gameObject.activeSelf)
                tipText.color = endColor;
            if (artText.gameObject.activeSelf)
                artText.color = endColor;
        }
 
        timer += 1f / (float)BattleConst.skillMotionFps * speedRatio;
    }
}