2018-12-21 unity 定时器 00:00格式

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

public class Timer : MonoBehaviour {
    int hour;
    int minute;
    int second;
    int millisecond;
    // 已经花费的时间
    float timeSpend = 0.0f;
    bool isStop;
    string timer;
    // Use this for initialization
    void Start () {
        
    }
    private void OnEnable()
    {
        timeSpend = 0;
        timer = "";
        isStop = true;
    }
    private void OnDisable()
    {
       
    }
    // Update is called once per frame
    void Update () {
        if(isStop)
        {
            return;
        }
        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);

        // string timer = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);
        timer = string.Format("{0:D2}:{1:D2}",minute, second);
       // Debug.Log(timer);
    }
    public string GetTimer()
    {
        return timer;
    }
    public void SopTimer()
    {
        isStop = true;
    }
    public void StartTimer()
    {
        isStop = false;
    }
}

你可能感兴趣的:(2018-12-21 unity 定时器 00:00格式)