当前位置:网站首页>Unity脚本API—Time类
Unity脚本API—Time类
2022-07-04 14:13:00 【@夜魅】
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;
}
}
}
边栏推荐
- 直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
- 华为云数据库DDS产品深度赋能
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- Comment configurer un accord
- Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
- 对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
- 一篇文章搞懂Go语言中的Context
- They are all talking about Devops. Do you really understand it?
- mysql 联合主键_Mysql 创建联合主键[通俗易懂]
- LeetCode 58. 最后一个单词的长度
猜你喜欢
Weibo and Huya advance into interest communities: different paths for peers
Is BigDecimal safe to calculate the amount? Look at these five pits~~
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
每周招聘|高级DBA年薪49+,机会越多,成功越近!
31年前的Beyond演唱会,是如何超清修复的?
深度学习 网络正则化
华为云数据库DDS产品深度赋能
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
03 storage system
Ali was laid off employees, looking for a job n day, headhunters came bad news
随机推荐
函数计算异步任务能力介绍 - 任务触发去重
What are the concepts of union, intersection, difference and complement?
Openresty redirection
Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000
Width accuracy
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
深度学习 网络正则化
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)
Graduation season - personal summary
开源人张亮的 17 年成长路线,热爱才能坚持
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
Numpy notes
[learning notes] matroid
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
进制形式
Partial modification - progressive development
各大主流编程语言性能PK,结果出乎意料