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
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM