ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Unity学习笔记】学会使用Time类中的基本属性和API

2022-07-21 12:01:30  阅读:204  来源: 互联网

标签:scale System Unity API Collections Time print using


一、时间缩放比例

        //时间停止
        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

标签:scale,System,Unity,API,Collections,Time,print,using
来源: https://www.cnblogs.com/ElecSheep/p/16501444.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有