Unity脚本API—Time类

Time类:

1、从Unity获取时间信息的接口

2、常用属性:

     time:从游戏开始到现在所用时间

     timeScale:时间缩放

     deltaTime:以秒为单位,表示每帧的经过时间

     unscaledDeltaTime:不受缩放影响的每帧经过时间

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

/// <summary>
/// 
/// </summary>
public class TimeDemo : MonoBehaviour
{
    public int speed = 100;
    public float a,b,c;
    public int count;
    public float du;
    public int timeVsCount;
    public int duVsCount;

    //渲染场景时执行,不受TimeScale影响
    public void Update()
    {
        a = Time.time;//受缩放影响的游戏运行时间
        b = Time.unscaledTime;//不受缩放影响的游戏运行时间
        c = Time.realtimeSinceStartup;//实际游戏运行时间

        //每渲染帧 执行1次 旋转1度
        //1秒 旋转?度  1秒内旋转帧数 相同的情况下
        //帧多 1秒旋转速度快 希望1帧旋转量小
        //  少           慢             大
        this.transform.Rotate(0, speed * Time.deltaTime,0);


        //游戏暂停,个别物体不受影响 Time.unscaledDeltaTime:不受缩放影响的每帧间隔
        this.transform.Rotate(0, speed * Time.unscaledDeltaTime, 0);

        //旋转/移动速度 * 每帧消耗时间,可以保证旋转/移动速度不受机器性能,以及渲染影响
        count++;
        du += speed * Time.deltaTime;
        if (a >= 1 && timeVsCount < 1) 
        {
            //speed:100 1秒渲染76次
            //speed:200 1秒渲染66次
            Debug.Log("游戏帧渲染了"+count+"次");
            Debug.Log("游戏对象旋转了" + du + "度");
            Debug.Log(Time.deltaTime);
            timeVsCount++;
        }

        if (du >= 360 && duVsCount < 1)
        {
            Debug.Log("游戏对象旋转一周,共计渲染量为" + count + "次");
            Debug.Log("游戏对象旋转了" + du + "度");
            Debug.Log(Time.deltaTime);
            duVsCount++;
        }
    }

    //固定 0.02 秒 执行一次 与渲染无关,受TimeScale影响
    public void FixedUpdate()
    {
        this.transform.Rotate(0,speed,0);
    }

    //游戏暂停
    private void OnGUI()
    {
        if (GUILayout.Button("暂停游戏")) 
        {
            Time.timeScale = 0;
        }
        if (GUILayout.Button("继续游戏"))
        {
            Time.timeScale = 1;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/LOVE_Me__/article/details/125463246