49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using DG.Tweening;
|
|
using System;
|
|
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<Text>();
|
|
}
|
|
private double temp = 0;
|
|
private int index = -1;
|
|
public void AnimateNum(float newScore,double totalDistance,int currentIndex, int digit, string format)
|
|
{
|
|
if (currentIndex != index)
|
|
{
|
|
text.text = string.Format(format, totalDistance);
|
|
index = currentIndex;
|
|
}
|
|
else
|
|
{
|
|
mScoreSequence.Append(DOTween.To(delegate (float value)
|
|
{
|
|
if (value != newScore)
|
|
{
|
|
temp = Math.Round(value, digit);
|
|
//向Text组件赋值
|
|
text.text = string.Format(format, temp);
|
|
}
|
|
}, mOldScore, newScore, 1f));
|
|
//将更新后的值记录下来, 用于下一次滚动动画
|
|
mOldScore = newScore;
|
|
}
|
|
}
|
|
}
|
|
}
|