美文网首页Unity的一些东西
Time类中的“大哥大”(⊙o⊙)

Time类中的“大哥大”(⊙o⊙)

作者: 夜行水寒 | 来源:发表于2017-06-14 00:01 被阅读19次

    Time.deltaTime的测试:

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
    
        public class timeTest : MonoBehaviour {
            public Transform cube;
         // Use this for initialization
         void Start () {
        }
          // Update is called once per frame
        void Update () {
             //Vector3.forward每次运动一米,这样一秒运动了五十米
              cube.Translate(Vector3.forward);
            //这样保证一秒执行一米(但是帧改变,这个值就会改变)
              cube.Translate(Vector3.forward / 50f);
            //保持一秒一米的移动(帧改变,值也不会改变了)
              cube.Translate(Vector3.forward * Time.deltaTime );
            //Time.timeScale 时间比例,默认为1
            //timeScale会对deltaTime产生影响
            //当<1,deltaTime值变小;当>1,deltaTime值变大
            //Time.deltaTime = Time.deltaTime * Time.timeScale
              Time.timeScale = 0.5f;
        }
        }
    

    Time.realtimeSinceStartup常用来测试方法的性能:因为可以获取方法执行前后的帧执行次数,相减就可以得到相应的时间。

        using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;
    
        public class timeTest : MonoBehaviour {
    
        public int runCount = 1000000;
        // Use this for initialization
        void Start () {
        float time1 = Time.realtimeSinceStartup;
        for (int i = 0; i < runCount; i++)
        {
            Method1();
    
        }
        float time2 = Time.realtimeSinceStartup;
        Debug.Log(time2 - time1);
    
        float time3 = Time.realtimeSinceStartup;
        for (int i = 0; i < runCount; i++)
        {
            Method2();
    
        }
        float time4 = Time.realtimeSinceStartup;
        Debug.Log(time4 - time3);
    
        }
    
        // Update is called once per frame
        void Update () {
    
        }
    
         void Method1() {
                    int i = 1 + 2;
         }
    
         void Method2() {
                    int j = 1 * 2;
         }
         }

    相关文章

      网友评论

        本文标题:Time类中的“大哥大”(⊙o⊙)

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