UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Time.deltaTime
# [Time](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time.html).deltaTime public static float **deltaTime** ; ## 描述 完成上一帧所用的时间(以秒为单位)(只读)。 此属性提供当前帧和上一帧之间的时间。 使用 [Time.deltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-deltaTime.html) 可在 `y` 方向以 `n` 单位/秒的速度移动 [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html)。将 `n` 乘以 [Time.deltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-deltaTime.html),然后与 `y` 分量相加。 [MonoBehaviour.FixedUpdate](https://docs.unity3d.com/cn/2019.4/ScriptReference/MonoBehaviour.FixedUpdate.html) 使用 [fixedDeltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-fixedDeltaTime.html),而非 [deltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-deltaTime.html)。不依赖于 [MonoBehaviour.OnGUI](https://docs.unity3d.com/cn/2019.4/ScriptReference/MonoBehaviour.OnGUI.html) 中的 [Time.deltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-deltaTime.html)。Unity 可以每帧多次调用 [OnGUI](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time.OnGUI.html)。应用程序每次调用使用相同的 [deltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-deltaTime.html) 值。 以下示例将实现一个 `timer`。`timer` 在每一帧增加 [deltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-deltaTime.html)。该示例声明 `timer` 值,并在达到 2 秒时将其重置为零。当 [MonoBehaviour.Update](https://docs.unity3d.com/cn/2019.4/ScriptReference/MonoBehaviour.Update.html) 增加 [deltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-deltaTime.html) 时,`timer` 不会达到 2.0。相应的测试适用于移动 2 秒的 `timer`。脚本代码将删除报告的时间,然后 `timer` 重新启动。重新启动时间并不总是精确到 0.0。速度在 0.5 到 2.0 秒之间变化。[Time.timeScale](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-timeScale.html) 用于存储所选择的时间流逝标度。 ``` using System.Collections; using System.Collections.Generic; using UnityEngine; // Time.deltaTime example. // // Wait two seconds and display waited time. // This is typically just beyond 2 seconds. // Allow the speed of the time to be increased or decreased. // It can range between 0.5 and 2.0. These changes only // happen when the timer restarts. public class ScriptExample : MonoBehaviour { private float waitTime = 2.0f; private float timer = 0.0f; private float visualTime = 0.0f; private int width, height; private float value = 10.0f; private float scrollBar = 1.0f; void Awake() { width = Screen.width; height = Screen.height; Time.timeScale = scrollBar; } void Update() { timer += Time.deltaTime; // Check if we have reached beyond 2 seconds. // Subtracting two is more accurate over time than resetting to zero. if (timer > waitTime) { visualTime = timer; // Remove the recorded 2 seconds. timer = timer - waitTime; Time.timeScale = scrollBar; } } void OnGUI() { GUIStyle sliderDetails = new GUIStyle(GUI.skin.GetStyle("horizontalSlider")); GUIStyle sliderThumbDetails = new GUIStyle(GUI.skin.GetStyle("horizontalSliderThumb")); GUIStyle labelDetails = new GUIStyle(GUI.skin.GetStyle("label")); // Set the size of the fonts and the width/height of the slider. labelDetails.fontSize = 6 * (width / 200); sliderDetails.fixedHeight = height / 32; sliderDetails.fontSize = 12 * (width / 200); sliderThumbDetails.fixedHeight = height / 32; sliderThumbDetails.fixedWidth = width / 32; // Show the slider. Make the scale to be ten times bigger than the needed size. value = GUI.HorizontalSlider(new Rect(width / 8, height / 4, width - (4 * width / 8), height - (2 * height / 4)), value, 5.0f, 20.0f, sliderDetails, sliderThumbDetails); // Show the value from the slider. Make sure that 0.5, 0.6... 1.9, 2.0 are shown. float v = ((float)Mathf.RoundToInt(value)) / 10.0f; GUI.Label(new Rect(width / 8, height / 3.25f, width - (2 * width / 8), height - (2 * height / 4)), "timeScale: " + v.ToString("f1"), labelDetails); scrollBar = v; // Display the recorded time in a certain size. labelDetails.fontSize = 14 * (width / 200); GUI.Label(new Rect(width / 8, height / 2, width - (2 * width / 8), height - (2 * height / 4)), "Timer value is: " + visualTime.ToString("f4") + " seconds.", labelDetails); } } ```
da
2022年6月21日 11:26
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码