Unity UICountUp脚本

参数说明

  • Txt:Text组件
  • Duration:持续时间
  • Count Up Curve:变化曲线

使用

点开折腾,例如:

效果

代码

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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UICountUp : MonoBehaviour {

public Text txt; //Text组件
public float duration; //持续时间
public AnimationCurve countUpCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1)); //变化曲线

public void CountUp() {
int startValue = 0; //起始值
int endValue = 100; //目标值
StartCoroutine(HandleCountUp(startValue, endValue));
}

IEnumerator HandleCountUp(int startValue, int endValue) {
for (float timer = 0; timer < duration; timer += Time.unscaledDeltaTime) {
float progress = timer / duration;
int value = (int)Mathf.Lerp(startValue, endValue, countUpCurve.Evaluate(progress));
txt.text = value.ToString();
yield return null;
}
}
}
作者

DullSword

发布于

2020-05-13

更新于

2024-07-02

许可协议

评论