Skip to main content

RealTimer

Important Attention Reminder

Coding Style wiki​


RealTimer is a real-time timer counted by the system clock (DateTime.Now), using the moment of instance creation as its time base. It is query-based and self-driven (no external Update feeding required), and is unaffected by Time.timeScale, game pause, or frame stalls. It supports the Timer (one-shot), Tick (repeating), and Mark (time anchor) modes plus time-speed adjustment.

NamespaceOxGKit.TimeSystem
Typepublic class RealTimer
SourceRealTimer.cs
using OxGKit.TimeSystem;

Quick Start​

using OxGKit.TimeSystem;
using UnityEngine;

public class RealCooldown : MonoBehaviour
{
private RealTimer _cooldown;

private void Start()
{
// Create and auto play (autoPlay: true)
this._cooldown = new RealTimer(true);
// Set a 10-second cooldown (real time)
this._cooldown.SetTimer(10f);
}

private void Update()
{
// Query-based check, no deltaTime feeding needed
if (this._cooldown.IsTimerTimeout())
{
// Cooldown finished (counts down even while Time.timeScale = 0)
}
}
}

General Rules​

RealTimer vs DeltaTimer​

Attention A RealTimer counts by the system clock — it keeps counting even while the game is paused (Time.timeScale = 0) or the app stalls. For timing that should follow game pause and speed changes, use a DeltaTimer instead (see Picking the Right Tool).

Playback State​

Important A timer is stopped by default after construction — call Play (or use the autoPlay: true constructor) to start counting.

Reminder Stop clears all timing data (keeps the time speed); Reset also resets the time speed back to its default; Pause only pauses and can be resumed with Play.


Constructors​

public RealTimer()

public RealTimer(bool autoPlay)

Creates the timer, using the system time at creation as the time base (internally runs Reset first); with autoPlay set to true, it starts counting immediately after creation.


Playback Control​

Method Overview​

MethodDescription
GetRealTimeGets the real seconds elapsed since instance creation.
PlayStarts or resumes counting.
PausePauses counting (resumable).
StopStops and clears timing data.
ResetResets all state (including time speed).
IsPlayingWhether the timer is counting.
IsPauseWhether the timer is paused (stopped).
GetTimeGets the timer's current time (seconds).
SetTimeSpeedSets the time speed.
GetTimeSpeedGets the time speed.

GetRealTime​

public float GetRealTime()

Gets the real seconds elapsed since the timer instance was created (computed from the system clock DateTime.Now, unaffected by the playback state and time speed).

Play​

public void Play()

Starts counting, or resumes from a paused state (the paused real-time span is deducted — the time does not jump).

Pause​

public void Pause()

Pauses counting and records the pause point; resumable with Play.

Stop​

public void Stop()

Stops counting and clears all timing data (Timer / Tick / Mark settings are zeroed), but keeps the time speed (timeSpeed).

Reset​

public void Reset()

Resets all state to defaults (including resetting the time speed to 1) and returns to the stopped state.

IsPlaying​

public bool IsPlaying()

Returns whether the timer is currently counting.

IsPause​

public bool IsPause()

Returns whether the timer is paused (not playing).

GetTime​

public float GetTime()

Gets the timer's current accumulated time (seconds); while counting it is affected by SetTimeSpeed, and while paused it returns the time at the pause point.

SetTimeSpeed​

public void SetTimeSpeed(float timeSpeed)

Sets the time speed (default 1), affecting the flow of GetTime (and therefore the Timer / Tick / Mark checks).

GetTimeSpeed​

public float GetTimeSpeed()

Gets the current time speed.


Timer (One-Shot)​

A one-shot countdown based on the configured seconds.

Method Overview​

MethodDescription
SetTimerSets the seconds to count.
TimerCountdownGets the remaining seconds (0 when reached).
IsTimerTimeoutReturns whether the timer has reached its time.
GetTimerCountdownRatioGets the countdown ratio (1 decreasing to 0).

SetTimer​

public void SetTimer(float timeSeconds)

Sets the seconds to count (current time + seconds becomes the trigger point).

this._realTimer.SetTimer(10f);
this._realTimer.Play();

TimerCountdown​

public float TimerCountdown()

Calculates the remaining seconds until the trigger point; returns 0 directly once the trigger time has been exceeded.

IsTimerTimeout​

public bool IsTimerTimeout()

Returns whether the timer has reached its time (true once past the trigger point).

Reminder After the time is up, IsTimerTimeout() keeps returning true — for one-shot behavior, call Stop or a new SetTimer after handling the trigger.

GetTimerCountdownRatio​

public float GetTimerCountdownRatio()

Gets the countdown ratio of the Timer, from 1 decreasing to 0, where 0 = time's up (handy for progress-bar UI).


Tick (Repeating)​

Fires repeatedly at the configured interval (re-arms automatically after each timeout).

Method Overview​

MethodDescription
SetTickSets the Tick interval in seconds.
GetTickGets the configured Tick interval.
TickCountdownGets the remaining seconds of the current Tick.
IsTickTimeoutReturns whether the Tick is due (auto re-arms).
GetTickCountdownRatioGets the Tick countdown ratio (1 decreasing to 0).

SetTick​

public void SetTick(float tickSeconds)

Sets the Tick interval in seconds; after IsTickTimeout fires, the Tick keeps cycling.

// Fire every 2 seconds (real time)
this._realTimer.SetTick(2f);
this._realTimer.Play();

GetTick​

public float GetTick()

Gets the configured Tick interval in seconds.

TickCountdown​

public float TickCountdown()

Gets the remaining seconds until the current Tick fires; returns 0 directly once the trigger time has been exceeded.

IsTickTimeout​

public bool IsTickTimeout()

Returns whether the Tick is due; when it returns true, the next Tick is automatically re-armed from the current time, forming a repeating trigger.

private void Update()
{
if (this._realTimer.IsPlaying() &&
this._realTimer.IsTickTimeout())
{
// Fires once per Tick cycle
}
}

Important When IsTickTimeout() returns true it re-arms automatically, which "consumes" that trigger — check it from exactly one caller per cycle, or the trigger will be eaten by another caller.

GetTickCountdownRatio​

public float GetTickCountdownRatio()

Gets the Tick countdown ratio, from 1 decreasing to 0, where 0 = time's up.


Mark (Time Anchor)​

Marks a point in time to measure elapsed time against (stopwatch-style).

Method Overview​

MethodDescription
SetMarkSets the mark to the current time.
GetMarkGets the marked time.
GetElapsedMarkTimeGets the elapsed seconds since the mark.

SetMark​

public void SetMark()

Sets the mark to the timer's current time.

GetMark​

public float GetMark()

Gets the marked time.

GetElapsedMarkTime​

public float GetElapsedMarkTime()

Gets the elapsed time (seconds) since the last mark; returns 0 while the current time has not passed the mark.

this._realTimer.SetMark();

// ... some time later
float elapsed = this._realTimer.GetElapsedMarkTime();