77 lines
2.1 KiB
C#
77 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
using TMPro;
|
|
public class Tools : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
public static IEnumerator AnimateText(float startValue, float endValue, float time, Text SetText)
|
|
{
|
|
float elapsedTime = 0f; // 已经过去的时间
|
|
while (elapsedTime < time)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float t = elapsedTime / time;
|
|
|
|
// 计算当前值,使用 Mathf.Lerp 进行线性插值
|
|
int currentValue = Mathf.RoundToInt(Mathf.Lerp(startValue, endValue, t));
|
|
|
|
// 更新文本显示
|
|
SetText.text = currentValue.ToString("F1");
|
|
|
|
yield return null; // 等待一帧
|
|
}
|
|
|
|
SetText.text = endValue.ToString("F1");
|
|
|
|
}
|
|
|
|
public static IEnumerator AnimateTextMashPro(float startValue, float endValue, float time, TextMeshProUGUI SetText)
|
|
{
|
|
float elapsedTime = 0f; // 已经过去的时间
|
|
while (elapsedTime < time)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float t = elapsedTime / time;
|
|
|
|
// 计算当前值,使用 Mathf.Lerp 进行线性插值
|
|
int currentValue = Mathf.RoundToInt(Mathf.Lerp(startValue, endValue, t));
|
|
|
|
// 更新文本显示
|
|
SetText.text = currentValue.ToString("F1");
|
|
|
|
yield return null; // 等待一帧
|
|
}
|
|
|
|
SetText.text = endValue.ToString("F1");
|
|
|
|
}
|
|
//public static void MoveUpOrDwon(RectTransform obj,float add)//向上或下移动
|
|
//{
|
|
// float targetY = obj.anchoredPosition.y + add; // 计算目标位置
|
|
// obj.DOAnchorPosY(targetY, 1f).SetEase(Ease.Linear);
|
|
|
|
//}
|
|
public static void MoveUpOrDwon(RectTransform obj, float add, System.Action onComplete = null)
|
|
{
|
|
float targetY = obj.anchoredPosition.y + add; // 计算目标位置
|
|
obj.DOAnchorPosY(targetY, 1f).SetEase(Ease.Linear).OnComplete(() => {
|
|
|
|
if (onComplete != null)
|
|
{
|
|
onComplete.Invoke();
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|