Unity之Time类

官方手册说明:https://docs.unity3d.com/cn/current/Manual/TimeFrameManagement.html

一、Time.time

该帧开始的时间(只读)。此为自游戏启动以来的时间(以秒为单位)

是应用程序已运行的时间(以秒为单位)。它是只读的。

应用程序在每帧开始时接收当前的 Time.time,该值按帧递增。每个帧的 time调用将接收相同的值。在从 FixedUpdate 中调用时,将返回Time.FixedUpdate属性。

应避免常规的(每帧)调用:Time.time倾向于提供应用程序已经运行的时间长度,而不是每帧的时间。

1.1Time.time就是为了提供游戏从开始到当前所花费的时间,单位秒。在编译器暂停时,是不会计算时间的

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

public class Test : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(AA());
    }

    void Update()
    {
    }

    IEnumerator AA() 
    {
        while (true)
        {
            Debug.Log(Time.time);
            yield return new WaitForSeconds(1f);
        }
    }

   
}

使用协程一秒打印一次Time.time结果

1.2 使用Time.time制作一秒执行一次

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine.UI;
using UnityEngine;

public class Test : MonoBehaviour
{
    float timerA;
    void Start()
    {
        timerA = Time.time;

        StartCoroutine(AA());
    }

    void Update()
    {
        if (Time.time- timerA>=1f)
        {
            Debug.Log("执行"+System.DateTime.Now);
            timerA = Time.time;
        }

    }
}

结果:

二、Time.deltatime

 返回自上一帧完成以来经过的时间量。

在Unity生命周期中Update的作用是每帧执行一次,但是因为电脑配置的不同,所以游戏开始时每秒渲染多少帧是不固定的。所以我们要想知道Update中每帧在1秒内的占比,就是Time.deltatime,Time.deltatime它的计算方式是 1秒/渲染帧数。

但是在FixedUpdate()中Time.deltatime返回的值与fixedDeltaTime一致

应用:

2.1物体匀速执行

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine.UI;
using UnityEngine;

public class Test : MonoBehaviour
{

    public GameObject cube;  
    public GameObject cube2; 
   
    void Start()
    {
        StartCoroutine(AA());
    }

    void Update()
    {
        //这里使用了Time.deltaTime 物体会匀速执行
        cube.transform.position += Vector3.one * Time.deltaTime;
    }

    IEnumerator AA() 
    {
        while (true)
        {
            //协程每1秒执行一次(因为生命周期执行顺序,位置会有一点小偏差)
            cube2.transform.position += Vector3.one;
            yield return new WaitForSeconds(1f);
        }
    }

   
}

效果:

 绿色代表1秒内要移动1米

灰色在update中1秒渲染了N帧,每一帧移动N米,得出一个匀速的效果。

三、Time.timeScale

表示时间流逝的速率。您可以读取此值,或将其设置为控制时间流逝的速度,从而创建慢动作效果。

Time.timeScale是用于控制任何刚体和依赖时间的函数、刚体力和速度等。只要是和时间有关的东西他都能“管”。

我们知道在Unity生命周期中有Update、LateUpdate、FixedUpdate三种函数,Time.timeScale只能控制FixedUpdate而其他两个则是不能,为什么呢?因为Update、LateUpdate含义是每帧执行一次,是由帧来决定,而帧数是由电脑的配置来决定(配置高的电脑帧数率高,游戏体验好),而FixedUpdate含义是根据固定时间来执行。

下面做个测试

结果:

 这里忘记打印按键了,其实可以明显看出当Time.timeScale = 0; //速度为0 FixedUpdate不执行其他两个都在执行。

3.2播放、暂停、加速效果

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

public class Test : MonoBehaviour
{
    public Text txt;
    public Transform cube;
    void Start()
    {
       
    }

    void Update() 
    {

        //按下0 
        if (Input.GetKeyDown(KeyCode.Alpha0))
        {
            Time.timeScale = 0; //速度为0 不执行
            txt.text = "暂停";
        }

        //按下1

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Time.timeScale = 1; //正常速度
            txt.text = "正常播放";

        }
        //按下2 
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            Time.timeScale = 2; //速度 2X
            txt.text = "2倍速";
        }

        cube.rotation *= Quaternion.Euler(Vector3.one*70 * Time.deltaTime); //使用了Time.deltaTime(时间) 所以受控制

    }

}

 效果

四、fixedDeltaTime

执行物理和其他固定帧率更新(如 MonoBehaviour 的 FixedUpdate)的时间间隔(以秒为单位)。

为了读取增量时间,建议改用 Time.deltaTime,因为当您位于 FixedUpdate 函数或 Update 函数中时, 它会自动返回正确的增量时间。

fixedDeltaTime只会返回一个固定的值(可以在Edit-->Project Settings-->Time面板中设置)

fixedDeltaTime控制的是FixedUpdate()的执行次数(每多少秒执行一次),而且在FixedUpdate()中调用的fixedDeltaTime都当1使用。

我们来测试一下

设置Time面板的 fixed Timestep值为1 即每1秒执行一次

 

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

public class Test : MonoBehaviour
{
    void Start()
    {
       
    }

    void FixedUpdate()
    {
        Debug.Log("测试1 "+1);
        Debug.Log("测试2 "+1*Time.fixedDeltaTime);
    }

}

 结果:

可以看到打印结果是相同的 

猜你喜欢

转载自blog.csdn.net/qq_42345116/article/details/122290856