public static float deltaTime ;

描述

完成上一帧所用的时间(以秒为单位)(只读)。

此属性提供当前帧和上一帧之间的时间。

使用 Time.deltaTime 可在 y 方向以 n 单位/秒的速度移动 GameObject。将 n 乘以 Time.deltaTime,然后与 y 分量相加。

MonoBehaviour.FixedUpdate 使用 fixedDeltaTime,而非 deltaTime。不依赖于 MonoBehaviour.OnGUI 中的 Time.deltaTime。Unity 可以每帧多次调用 OnGUI。应用程序每次调用使用相同的 deltaTime 值。

以下示例将实现一个 timertimer 在每一帧增加 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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961