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





    作者:艾孜尔江

  • 相关阅读:
    Flex布局-语法篇
    css grid布局
    js 方法的一些简写和技巧
    css瀑布流
    js防抖和节流
    js循环
    两行css代码实现居中元素
    手写代码部分
    BigDecimal类的概念和使用
    Math类的概念和使用
  • 原文地址:https://www.cnblogs.com/ezhar/p/14223072.html
Copyright © 2011-2022 走看看