81 lines
2.7 KiB
C#
81 lines
2.7 KiB
C#
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 Sequence mProgressSequence;
|
|
private Text text;
|
|
private float mOldScore = 0;
|
|
private float mOldScoreNew = 0;
|
|
|
|
private void Start()
|
|
{
|
|
//函数内初始化
|
|
mScoreSequence = DOTween.Sequence();
|
|
mProgressSequence = DOTween.Sequence();
|
|
//函数内设置属性
|
|
mScoreSequence.SetAutoKill(false);
|
|
mProgressSequence.SetAutoKill(false);
|
|
text = transform.GetComponent<Text>();
|
|
}
|
|
private double temp = 0;
|
|
private int index = -1;
|
|
private int indexNew = -1;
|
|
public void AnimateNum(float newScore,double totalDistance,int currentIndex, int digit, string format,Image process)
|
|
{
|
|
if (currentIndex != indexNew)
|
|
{
|
|
indexNew = currentIndex;
|
|
process.fillAmount = 1;
|
|
StartCoroutine(CompleteFill(process));
|
|
}
|
|
else
|
|
{
|
|
mProgressSequence.Append(DOTween.To(delegate (float value)
|
|
{
|
|
process.fillAmount = (float)(value / totalDistance);
|
|
}, mOldScoreNew, newScore, 1f));
|
|
//将更新后的值记录下来, 用于下一次滚动动画
|
|
mOldScoreNew = newScore;
|
|
}
|
|
}
|
|
|
|
public void AnimateNumText(float newScore, double totalDistance, int currentIndex, int digit, string format, Image process)
|
|
{
|
|
if (currentIndex != index)
|
|
{
|
|
text.text = string.Format(format, totalDistance);
|
|
index = currentIndex;
|
|
mOldScore = (float)totalDistance;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
IEnumerator CompleteFill(Image process)
|
|
{
|
|
yield return new WaitForSeconds(0.3f);
|
|
mOldScoreNew = 0;
|
|
process.fillAmount = 0;
|
|
}
|
|
}
|
|
}
|