Skip to main content

Module Intro

Important Attention Reminder

Coding Style wiki


Basic Instructions

Various time controllers: DeltaTimer, RealTimer, DTUpdater, RTUpdater, IntervalTimer, IntervalSetter, NtpTime (clock sync with NTP server) — covering game-time and real-time timers, update loops independent of MonoBehaviour, a JavaScript-setInterval-style periodic ticker, and a clock synchronization tool for NTP servers.

  • Version: v1.1.3 (com.michaelo.oxgkit.timesystem)
  • Namespace: OxGKit.TimeSystem

Attention This module depends on UniTask and OxGKit.LoggingSystem (as log output) — install the dependencies first.


Application Description

Picking the Right Tool (by Time Base)

Important Before choosing a timer or updater, decide whether the use case must ignore Time.timeScale and game pause (real time) or should follow game time — that decides the Real series vs the Delta (DT) series.

TypeTime BaseDriven ByUse For
DeltaTimerAccumulated deltaTime fed externally (game time)Owner calls UpdateTimer(dt) in UpdateCooldowns/countdowns that follow game pause and speed changes
RealTimerSystem clock (real time)Self-driven (query-based)Cooldowns/countdowns unaffected by pause and Time.timeScale
DTUpdaterUniTask update loop (multiplies Unity Time.timeScale)Start()Standalone update loops affected by Time.timeScale
RTUpdaterUniTask update loop (real time)Start() / StartOnThread()Standalone update loops unaffected by Time.timeScale (e.g. network heartbeat)
IntervalTimer / IntervalSetterMillisecond intervalSetIntervalFire an Action every N milliseconds (JavaScript-setInterval style)
RealTimeSystem clockMain program calls InitStartupTime()Recording the application startup time
NtpTimeNTP server / HTTP time APISynchronize()Server-authoritative time (anti-cheat, daily reset, and other trusted-time scenarios)

Timer / Tick / Mark Timing Modes

Both DeltaTimer and RealTimer provide three timing modes that can be used simultaneously on the same instance:

  • Timer (one-shot): set a one-shot countdown with SetTimer(seconds) and check it with IsTimerTimeout().
  • Tick (repeating): set a repeating interval with SetTick(seconds); when IsTickTimeout() returns true it re-arms automatically, firing in a loop.
  • Mark (time anchor): mark the current time with SetMark() and read the elapsed time since the mark with GetElapsedMarkTime() (stopwatch-style).

Update Loops and Interval Ticking

  • DTUpdater and RTUpdater are UniTask-driven standalone update loops exposing onUpdate, onFixedUpdate, and onLateUpdate callbacks, with a configurable targetFrameRate (loop frequency) and timeScale (own time scale, 0 ~ 64) — great for driving non-MonoBehaviour systems (e.g. an ActionSystem runner).
  • IntervalSetter is a static manager that creates and manages IntervalTimer instances, keyed by int id / string id / anonymous handle.

Important Updaters and IntervalTimers are UniTask loops — when the owning feature or scene exits, always call Stop() / StopInterval() / TryClearInterval(), otherwise the loops keep running.

Module Logs

  • This module logs through OxGKit.LoggingSystem with the logger name OxGKit.TimeSystem.Logger; its toggle and level can be configured on the LoggingLauncher.

Simple Usage

RealTimer vs DeltaTimer

using OxGKit.TimeSystem;
using UnityEngine;

public class TimerExample : MonoBehaviour
{
private RealTimer _realTimer;
private DeltaTimer _deltaTimer;

private void Start()
{
// RealTimer (real time, unaffected by Time.timeScale and pause)
this._realTimer = new RealTimer();
this._realTimer.SetTimer(3f);
this._realTimer.Play();

// DeltaTimer (game time, driven by feeding dt via UpdateTimer)
this._deltaTimer = new DeltaTimer();
this._deltaTimer.SetTimer(3f);
this._deltaTimer.Play();
}

private void Update()
{
// A DeltaTimer must be fed deltaTime continuously
this._deltaTimer.UpdateTimer(Time.deltaTime);

// Check the RealTimer
if (this._realTimer.IsPlaying() &&
this._realTimer.IsTimerTimeout())
{
this._realTimer.Stop();
Debug.Log("[RealTimer] Time's up!");
}

// Check the DeltaTimer
if (this._deltaTimer.IsPlaying() &&
this._deltaTimer.IsTimerTimeout())
{
this._deltaTimer.Stop();
Debug.Log("[DeltaTimer] Time's up!");
}
}
}

Updater Loop

using OxGKit.TimeSystem;

// Update loop independent of MonoBehaviour (UniTask-driven)
var rtUpdater = new RTUpdater();
rtUpdater.onUpdate += (dt) => { /* per update */ };
rtUpdater.timeScale = 1f; // own time scale (0 ~ 64)
rtUpdater.targetFrameRate = 60f; // loop frequency (default 60)
rtUpdater.Start();

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

IntervalSetter Ticking

using OxGKit.TimeSystem;

// Fire every 1000 milliseconds (managed by string id)
IntervalSetter.SetInterval("poll", () => { /* periodic work */ }, 1000);

// Stop and remove
IntervalSetter.TryClearInterval("poll");

NtpTime Clock Sync

using OxGKit.TimeSystem;

// Synchronize with an NTP server (default time.google.com)
NtpTime.Synchronize();

// Read the corrected time once synchronized
if (NtpTime.IsSynchronized())
{
var now = NtpTime.GetNow();
var utcNow = NtpTime.GetUtcNow();
}

[See Example]


Installation

Install via git URL
Add https://github.com/michael811125/OxGKit.git?path=Assets/OxGKit/TimeSystem/Scripts to Package Manager

Third-party libraries (install separately)

Samples (Package Manager -> Samples)


Module API


Demo

TimeSystem Demo