zoukankan      html  css  js  c++  java
  • Unity设置计时器

    方法一:

    
    using UnityEngine;
    using UnityEngine.UI;
    
    public class TimeUpdatation : MonoBehaviour
    {
        /// Way 2
        int hour;
        int minute;
        int second;
        int millisecond;
        bool shouldCount = false;
    
        float timeSpent = 0.0f;
    
    
    
        void OnEnable()
        {
            shouldCount = true;
        }
    
        void Update()
        {
            if (shouldCount)
            {
                timeSpent += Time.deltaTime;
    
                hour = (int)timeSpent / 3600;
                minute = ((int)timeSpent - hour * 3600) / 60;
                second = (int)timeSpent - hour * 3600 - minute * 60;
                millisecond = (int)((timeSpent - (int)timeSpent) * 1000);
    
                //text_timeSpent.text = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);
                GetComponent<Text>().text = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);
            }
        }
    
        //void OnDestroy
        void OnDisable()
        {
            shouldCount = false;
            GetComponent<Text>().text = "";
        }
    }
    


    方法二:

    using UnityEngine;
    using UnityEngine.UI;
    
    public class TimeUpdatation : MonoBehaviour
    {
        /// Way 1
        public int frameRate = 30;
    
        void Update()
        {
            var t = Time.unscaledTime;
            var seconds = (int)t;
            var h = seconds / 3600;
            var hInSec = h * 3600;
            var m = (seconds - hInSec) / 60;
            var mInSec = m * 60;
            var s = (seconds - (hInSec + mInSec));
            var framesDouble = frameRate * (t - (double)seconds);
            var f = (int)framesDouble;
            //var frac = 100 * (framesDouble - f) / frameRate;
            GetComponent<Text>().text = $"{h:00}:{m:00}:{s:00}:{f:00}";
        }
    }
    





    作者:艾孜尔江

  • 相关阅读:
    BZOJ 2016十连测 D3T3序列
    Luogu3579 Solar Panels
    POI2014解题报告
    BZOJ4377 Kurs szybkiego czytania Luogu 3589[POI2015]KUR
    Luogu3587[POI2015]POD
    BZOJ4386[POI2015]Wycieczki / Luogu3597[POI2015]WYC
    BZOJ4381 : [POI2015]Odwiedziny / Luogu3591[POI2015]ODW
    HDU 1133 Buy the ticket
    HDU RPG的错排 【错排&&组合】
    【转】求逆元的n种方法
  • 原文地址:https://www.cnblogs.com/ezhar/p/14223072.html
Copyright © 2011-2022 走看看