unity毫秒倒计时效果实现

脚本附加在一个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;
            }

        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值