一、时间缩放比例

        //时间停止
        Time.timeScale = 0;
        //恢复正常
        Time.timeScale = 1;
        //2倍速
        Time.timeScale = 2;

二、帧间隔时间

帧间隔时间:最近的一帧用了多 长时间(单位 秒)
作用:主要是用来计算位移的 (路程 = 时间 * 速度)
根据需求选择受scale影响的和不受scale影响的
如果希望游戏暂停时就不动,那就使用deltaTime
如果希望游戏暂停了 也继续跑,那就使用unscaleDeltaTime

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

public class Lesson5 : MonoBehaviour
{
    private void Update()
    {
        //为了体现两个的区别,现在把时间缩放比例设置成:时间暂停
        Time.timeScale = 0;

        //1.受时间缩放比例(scale)影响的
        print("帧间隔时间:" + Time.deltaTime);

        //2.不受时间缩放比例(scale)影响的
        print("不受scale影响的帧间隔时间:" + Time.unscaledDeltaTime);
    }
}

运行:
image

三、游戏开始到现在的时间

作用:主要用来计时(一般用在单机游戏中)

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

public class Lesson5 : MonoBehaviour
{
    private void Update()
    {
        //为了体现两个的区别,现在把时间缩放比例设置成:时间暂停
        Time.timeScale = 0;

        //1.受scale影响的
        print("受scale影响的时间:" + Time.time);

        //2.不受scale影响的
        print("不受scale影响的时间:" + Time.unscaledTime);
    }
}

运行:
image

四、物理间隔时间 FixedUpdate

注意:一般写在生命周期函数FixedUpDate中
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson5 : MonoBehaviour
{
    private void Update()
    {
        //为了体现两个的区别,现在把时间缩放比例设置成:时间暂停
        Time.timeScale = 0;

        //1.受scale影响的
        print("受scale影响的时间:" + Time.fixedDeltaTime);

        //2.不受scale影响的
        print("不受scale影响的时间:" + Time.fixedUnscaledDeltaTime);
    }
}

运行:
image

五、游戏开始到现在跑了多少帧

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

public class Lesson5 : MonoBehaviour
{
    private void Update()
    {
        print("游戏开始到现在跑的帧数是:" + Time.frameCount);
    }
}

运行:
image

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/ElecSheep/p/16501444.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!