发表更新1 分钟读完 (大约136个字)0次访问
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; 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; } } }
|