美文网首页
2018-12-21 unity 定时器 00:00格式

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

作者: 流光念岁月 | 来源:发表于2018-12-21 13:45 被阅读0次
    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格式

          本文链接:https://www.haomeiwen.com/subject/fppmkqtx.html