Unity 6.3
0 онлайн 8 гостей 3 в системе
Вход

Time.deltaTime

Declaration

public static float deltaTime;

Описание

Интервал в секундах между последним кадром и текущим (только для чтения).

При вызове изнутри MonoBehaviour.FixedUpdate или в любом месте в цикле обновления Physics, включая сопутствующие программы, WaitForFixedUpdate, deltaTime возвращений Time.fixedDeltaTime. Максимальное значение для deltaTime определяется как Time.maximumDeltaTime.

Стоимость deltaTime масштабируется в зависимости от Time.timeScale. Если игра приостановлена настройкой Time.timeScale до 0, то deltaTime также становится 0. Первые кадры после смены Time.timeScale может привести к неожиданным deltaTime значения. Использование Time.unscaledDeltaTime вместо этого, если вам нужны стенные часы во время паузы.

Стоимость deltaTime может быть ненадежным в обратных вызовах, которые могут вызываться несколько раз в кадре, таких как MonoBehaviour.OnGUI или UI события раскладки или перекраски.

Первый кадр Update может привести к очень небольшому или нулевому deltaTime на некоторых платформах, так же как и первый кадр после перезагрузки домена и сцены.

В зависимости от платформы и Application.runInBackground настройка, когда приложение теряет фокус или находится в фоновом режиме, Update может вызывать редко или с большими промежутками времени, deltaTime ценностей.

Следующий пример вращает a GameObject двигается вокруг своей оси z с постоянной скоростью.

Дополнительные сведения о том, как это свойство связано с другими свойствами времени, см. Управление временем и частотой кадров в 2000 году Unity Руководство.

using UnityEngine;
// Rotate around the z axis at a constant speed
public class ConstantRotation : MonoBehaviour
{
    public float degreesPerSecond = 2.0f;
    void Update()
    {
        transform.Rotate(0, 0, degreesPerSecond * Time.deltaTime);
    }
}

В следующем примере реализован таймер. Таймер добавляет deltaTime к каждому кадру. Пример отображает значение таймера и сбрасывает его, когда оно достигает 2 секунд. 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); } }