using DG.Tweening; using System; using System.Collections; using System.Linq; using UnityEngine; using UnityEngine.UI; namespace Assets.Scenes.Ride.Scripts { public class NumberDotween:MonoBehaviour { private Sequence mScoreSequence; private Text text; private float mOldScore = 0; private void Start() { //函数内初始化 mScoreSequence = DOTween.Sequence(); //函数内设置属性 mScoreSequence.SetAutoKill(false); text = transform.GetComponent(); } private double temp = 0; private int index = -1; public void AnimateNum(float newScore,double totalDistance,int currentIndex, int digit, string format,Image process) { if (currentIndex != index) { text.text = string.Format(format, totalDistance); index = currentIndex; process.fillAmount = 1; StartCoroutine(CompleteFill(process)); } else { mScoreSequence.Append(DOTween.To(delegate (float value) { temp = Math.Round(value, digit); process.fillAmount = (float)(value / totalDistance); //向Text组件赋值 text.text = string.Format(format, temp); }, mOldScore, newScore, 1f)); //将更新后的值记录下来, 用于下一次滚动动画 mOldScore = newScore; } } IEnumerator CompleteFill(Image process) { yield return new WaitForSeconds(0.3f); mOldScore = 0; process.fillAmount = 0; } } }