Unity 秒表的实现 / 倒计时的实现

下面直接上代码   

1.计时器的实现   第一种方法

  /// <summary>
    /// 秒表的实现方法      创建一个Text  挂在上面即可
    /// </summary>
    private float timeSpend = 0;
    private int hour;   //小时
    private int minute; //分钟
    private int second; //
    private int millisecond; //毫秒
    private Text recealText;  //显示的text

    private void Start()
    {
        recealText = this.transform.GetComponent<Text>();
    }
    // Update is called once per frame
    void Update()
    {
        timeSpend += Time.deltaTime;
        hour = (int)timeSpend / 3600;
        minute = ((int)timeSpend - hour * 3600) / 60;
        second = (int)timeSpend - hour * 3600 - minute * 60;
        millisecond = (int)((timeSpend - (int)timeSpend) * 1000);

        recealText.text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", hour, minute, second, millisecond);

    }

第二种方法实现   

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

public class Timer_Clock : MonoBehaviour
{
    /// <summary>
    /// 秒表的实现方法      创建一个Text  挂在上面即可
    /// </summary>
    private float timeSpend = 0;
    private int hour;   //小时
    private int minute; //分钟
    private int second; //
    private int millisecond; //毫秒
    public Text recealText;  //显示的text
    public bool mator = false;
    private void Start()
    {
        recealText = this.transform.GetComponent<Text>();
        StartCoroutine(timerr ());
    }
    IEnumerator timerr()
    {
        while (!mator)
        {
            timeSpend += Time.deltaTime;
            hour = (int)timeSpend / 3600;
            minute = ((int)timeSpend - hour * 3600) / 60;
            second = (int)timeSpend - hour * 3600 - minute * 60;
            millisecond = (int)((timeSpend - (int)timeSpend) * 1000);

            recealText.text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", hour, minute, second, millisecond);
            yield return new WaitForFixedUpdate();
        }          
    }
    public void OnDestyoy_OBJ()
    {
        Destroy(this.gameObject);
    }
}

 

2.倒计时的实现  

创建一个Text 脚本挂在上面即可

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 倒计时的实现
/// </summary>
public class CountDownTime : MonoBehaviour
{
    private Text text;
    int Shi;
    int Fen;
    int miao;
    private bool aa;
    public float time;
    // Start is called before the first frame update
    void Start()
    {
        text = this.GetComponent<Text>();
        StartCoroutine("enumerator");

        //Endkisijd();
    }
   
    public void Endkisijd()
    {           
        aa = true;      
    }
    #region   第一种方法
    IEnumerator enumerator()
    {      
        while (time >= 0)
        {
            Shi = (int)time / 3600;
            Fen = (int)time % 3600 / 60;
            miao = (int)time % 60;
            text.text = string.Format ("{0}:{1}:{2}",Shi,Fen ,miao );
            yield return new WaitForSeconds(1);
            time--;
        }
    }
    #endregion
    // Update is called once per frame
    void Update()
    {
        #region 第二种方法
        /*         
        if (aa)
        {
            Shi = (int)time / 3600;
            Fen = (int)time % 3600 / 60;
            miao = (int)time % 60;
            text.text = string.Format("{0}:{1}:{2}", Shi, Fen, miao);
        }
        if (time > 0)
        {
            time -= Time.deltaTime;
        }
        else
        {
            text.text = string.Format("{0}:{1}:{2}", 00, 00, 00);
            aa = false;
        }      
        */
        #endregion
    }
}

 

posted @ 2020-10-12 10:45  剑起苍穹  阅读(1686)  评论(0编辑  收藏  举报
/*鼠标点击特效*/