完成上一帧所用的时间(以秒为单位)(只读)。
此属性提供当前帧和上一帧之间的时间。
使用 Time.deltaTime 可在 y
方向以 n
单位/秒的速度移动 GameObject。将 n
乘以 Time.deltaTime,然后与 y
分量相加。
MonoBehaviour.FixedUpdate 使用 fixedDeltaTime,而非 deltaTime。不依赖于 MonoBehaviour.OnGUI 中的 Time.deltaTime。Unity 可以每帧多次调用 OnGUI。应用程序每次调用使用相同的 deltaTime 值。
以下示例将实现一个 timer
。timer
在每一帧增加 deltaTime。该示例声明 timer
值,并在达到 2 秒时将其重置为零。当 MonoBehaviour.Update 增加 deltaTime 时,timer
不会达到 2.0。相应的测试适用于移动 2 秒的 timer
。脚本代码将删除报告的时间,然后 timer
重新启动。重新启动时间并不总是精确到 0.0。速度在 0.5 到 2.0 秒之间变化。Time.timeScale 用于存储所选择的时间流逝标度。
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); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.