脚本附加在一个Text上,注意新版和旧版unityText的不同
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
///summary
///
///summary
public class CountDownTime : MonoBehaviour
{
private TextMeshProUGUI txtTimer;
private int timer = 120000;
private RectTransform Rtf;
void Start()
{
txtTimer=GetComponent<TextMeshProUGUI>();
Rtf = GetComponent<RectTransform>();
InvokeRepeating("CountDown", 0.001f, 0.01f); //第一次 0.001秒调用,剩下重复0.01秒调用
}
private void CountDown()
{
int minus = timer / 1000 / 60; //分钟数
int second = timer / 1000 % 60; //秒数
txtTimer.text = string.Format("{0:d2}:{1:d2}:{2:d2}", minus,second,timer%1000); //修改文本
timer-=10; //每次减少10毫秒
if (minus==0&&second<=10)
{
txtTimer.color = Color.red;
Rtf.localScale = new Vector3(1.2f, 1.2f, 1.2f);
if (timer <= 0) //时间归零
{
// 取消调用
CancelInvoke("CountDown");
return;
}
}
}
}