unity 顯示已使用時間(格式:分秒)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
    private float nowTime;
    public Text timeText;
    public bool isStart=false;

    void Update()
    {
        if (isStart)
        {
            nowTime += Time.deltaTime;
            int hour = (int)nowTime / 3600;
            int minute = (int)(nowTime - hour * 3600) / 60;
            int second = (int)(nowTime - hour * 3600 - minute * 60);
            timeText.text = string.Format("{0:D2}:{1:D2}", minute, second);
        }      
    }

    

    //開始計時
    public  void startJiShi()
    {
        nowTime = 0;
        isStart = true;
    }

    //停止計時
    public void tingZhiJiShi()
    {
        isStart = false;
    }

}

做出的效果如下:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章