Skip to main content

DTUpdater

Important Attention Reminder

Coding Style wiki


DTUpdater is an update loop independent of MonoBehaviour (UniTask-driven), exposing onUpdate, onFixedUpdate, and onLateUpdate callbacks with a configurable targetFrameRate (loop frequency) and timeScale (own time scale). The loop frequency is multiplied by Unity's Time.timeScale — with Time.timeScale = 0 the loop idles (follows game pause) — making it suitable for driving non-MonoBehaviour systems that should follow game time.

NamespaceOxGKit.TimeSystem
Typepublic class DTUpdater
SourceDTUpdater.cs
using OxGKit.TimeSystem;

Quick Start

using OxGKit.TimeSystem;

var dtUpdater = new DTUpdater();

// Subscribe update callbacks
dtUpdater.onUpdate += (dt) => { /* per update (actual elapsed time) */ };
dtUpdater.onFixedUpdate += (fdt) => { /* fixed-interval update */ };
dtUpdater.onLateUpdate += (dt) => { /* late update */ };

// Configurable loop frequency and time scale
dtUpdater.targetFrameRate = 60f; // default 60
dtUpdater.timeScale = 1f; // 0 ~ 64, default 1

// Start the update loop
dtUpdater.Start();

// Always stop when the feature exits
dtUpdater.Stop();

General Rules

Loop Frequency and Time Scaling

  • Per cycle, the loop frequency = targetFrameRate × (Time.timeScale × timeScale), and the wait interval is fixedDeltaTime = 1 / frequency.
  • Each cycle fires in order: onFixedUpdate(fixedDeltaTime)onUpdate(deltaTime)onLateUpdate(deltaTime), where deltaTime is the actual elapsed time of the previous cycle measured by the system clock, and fixedDeltaTime is the fixed interval value.

Attention The loop only runs while Time.timeScale is greater than 0 AND targetFrameRate is greater than 0 AND timeScale is greater than 0; if any of them is 0, the loop idles (no callbacks) — so a DTUpdater follows Unity game pause. For an update loop unaffected by Time.timeScale, use an RTUpdater instead (see Picking the Right Tool).

Lifecycle

Important The update loop is a UniTask loop — when the owner (feature/scene) exits, always call Stop, otherwise the loop keeps running; if a callback throws inside the loop, the updater stops automatically and rethrows.


Delegates

public delegate void DeltaTimeUpdate(float deltaTime);
public delegate void DeltaTimeFixedUpdate(float fixedDeltaTime);
public delegate void DeltaTimeLateUpdate(float deltaTime);

Members

MemberDescription
public DeltaTimeUpdate onUpdatePer-cycle update callback (parameter is the previous cycle's actual elapsed deltaTime).
public DeltaTimeFixedUpdate onFixedUpdatePer-cycle fixed-interval callback (parameter is fixedDeltaTime).
public DeltaTimeLateUpdate onLateUpdatePer-cycle late-update callback (fires after onUpdate).
public float timeSinceStartup { get; }Elapsed time since instance creation (seconds, system clock).
public float timeAtLastFrame { get; }Time point of the previous cycle (seconds).
public float timeScale { get; set; }Own time scale; assigned values are clamped to 0 ~ 64 (default 1).
public float targetFrameRate { get; set; }Target loop frequency (times per second); negative values are clamped to 0 (default 60).
public float deltaTime { get; }Actual elapsed time of the previous cycle (seconds).
public float fixedDeltaTime { get; }Current fixed interval (1 / frequency).

Constructors

public DTUpdater()

Creates the updater, using the system time at creation as the base for timeSinceStartup.


Methods

Method Overview

MethodDescription
StartStarts the update loop.
StopStops the update loop.
IsRunningWhether the loop is running.

Start

public void Start()

Starts the update loop (UniTask); calling it again while already running has no effect.

Stop

public void Stop()

Stops the update loop and cancels the internal CancellationTokenSource; after stopping, Start can be called again to restart.

IsRunning

public bool IsRunning()

Returns whether the update loop is currently running.