您当前的位置: 首页 >  Peter_Gao_ 游戏

实现Unity游戏倒计时的两种方法

Peter_Gao_ 发布时间:2020-04-18 19:35:04 ,浏览量:4

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UIFrame;
using DemoProject;
using UnityEngine.UI;


public class CountDownTimer : MonoBehaviour
{
    private int totaltime1 = 5;
    private int totaltime2 = 6;
    private float intervaletime=1;
    public Text countdown1text;
    public Text countdown2text;

    void Start()
    {
        countdown1text.text = string.Format("{0:00}:{1:00}", (int)totaltime1 / 60, (float)totaltime1 % 60);
        countdown2text.text = string.Format("{0:00}:{1:00}", (int)totaltime2 / 60, (float)totaltime2 % 60);
        StartCoroutine(CountDown());
    }

    // 方法一:用IEnumerator协程迭代器实现倒计时器
    IEnumerator CountDown()
    {
        while (totaltime1 >=0)  // 注意这里的死循环只能放协程里,不能放到外面主线程,会卡死
        {
            int M = (int)(totaltime1 / 60);
            float S = totaltime1 % 60;

            // 显示格式为 M分:S秒
            countdown1text.GetComponent().text = string.Format("{0:00}:{1:00}", M,S);
            // 每一帧update后等待1秒延迟再继续下一帧
            yield return new WaitForSeconds(1);
            // 时间减去一秒
            totaltime1--;

            // 计时器复位重新开始计时

            if (totaltime1 < 0)
            {
                totaltime1 = 5;
            }
        }
    }


    // 方法二:采用Update方法实现倒计时
    // Update is called once per frame
    void Update()
    {
        
            int M = (int)(totaltime2 / 60);
            float S = totaltime2 % 60;
            if (totaltime2 > 0)
            {
                intervaletime -= Time.deltaTime;
                if (intervaletime

关注
打赏
查看更多评论

Peter_Gao_

暂无认证

  • 4浏览

    0关注

    534博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录