当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢

.Net 应用考虑x64生成
![LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路](/img/4a/6763e3fbdeaf9de673fbe8eaf96858.png)
LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路

从0到1建设智能灰度数据体系:以vivo游戏中心为例

Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture

夜天之书 #53 Apache 开源社群的“石头汤”

Halo effect - who says that those with light on their heads are heroes

Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing

Helix swarm Chinese package is released, and perforce further improves the user experience in China

Why do domestic mobile phone users choose iPhone when changing a mobile phone?

How to build a technical team that will bring down the company?
随机推荐
Luo Gu - some interesting questions
[learning notes] matroid
03-存储系统
MySQL学习笔记——数据类型(2)
Deep learning neural network case (handwritten digit recognition)
Align left and right!
Ali was laid off employees, looking for a job n day, headhunters came bad news
Optimization method of deep learning neural network
中国主要城市人均存款出炉,你达标了吗?
深度学习 神经网络案例(手写数字识别)
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
数据库函数的用法「建议收藏」
[local differential privacy and random response code implementation] differential privacy code implementation series (13)
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
Redis 发布和订阅
近一亿美元失窃,Horizon跨链桥被攻击事件分析
Openresty redirection
深度学习 网络正则化
CentOS 6.3 下 PHP编译安装JSON模块报错解决
如何配和弦