Module Intro
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.
| Type | Time Base | Driven By | Use For |
|---|---|---|---|
| DeltaTimer | Accumulated deltaTime fed externally (game time) | Owner calls UpdateTimer(dt) in Update | Cooldowns/countdowns that follow game pause and speed changes |
| RealTimer | System clock (real time) | Self-driven (query-based) | Cooldowns/countdowns unaffected by pause and Time.timeScale |
| DTUpdater | UniTask update loop (multiplies Unity Time.timeScale) | Start() | Standalone update loops affected by Time.timeScale |
| RTUpdater | UniTask update loop (real time) | Start() / StartOnThread() | Standalone update loops unaffected by Time.timeScale (e.g. network heartbeat) |
| IntervalTimer / IntervalSetter | Millisecond interval | SetInterval | Fire an Action every N milliseconds (JavaScript-setInterval style) |
| RealTime | System clock | Main program calls InitStartupTime() | Recording the application startup time |
| NtpTime | NTP server / HTTP time API | Synchronize() | 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 withIsTimerTimeout(). - Tick (repeating): set a repeating interval with
SetTick(seconds); whenIsTickTimeout()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 withGetElapsedMarkTime()(stopwatch-style).
Update Loops and Interval Ticking
- DTUpdater and RTUpdater are UniTask-driven standalone update loops exposing
onUpdate,onFixedUpdate, andonLateUpdatecallbacks, with a configurabletargetFrameRate(loop frequency) andtimeScale(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)
- Use UniTask v2.5.0 or higher, Add https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask to Package Manager
- Use OxGKit.LoggingSystem, Add https://github.com/michael811125/OxGKit.git?path=Assets/OxGKit/LoggingSystem/Scripts to Package Manager
Samples (Package Manager -> Samples)
- AI Agent Skills (see AI Agent Skills)
- Timer Demo
Module API
- Runtime
- DeltaTimer (using OxGKit.TimeSystem)
- RealTimer (using OxGKit.TimeSystem)
- DTUpdater (using OxGKit.TimeSystem)
- RTUpdater (using OxGKit.TimeSystem)
- IntervalTimer (using OxGKit.TimeSystem)
- IntervalSetter (using OxGKit.TimeSystem)
- RealTime (using OxGKit.TimeSystem)
- NtpTime (using OxGKit.TimeSystem)
Demo
TimeSystem Demo