Class UnityTimerService
Unity-specific timer service that manages multiple timer instances as MonoBehaviour components.
Provides centralized timer management with creation, retrieval, and removal capabilities.
Namespace: Serenity.Timer.Infrastructure.Components
Assembly: Serenity.UnityTimer.Infrastructure.dll
Syntax
public class UnityTimerService : MonoBehaviour, ITimerService, IService, IFoundationSettings
Constructors
UnityTimerService()
Declaration
public UnityTimerService()
Fields
_guid
Declaration
protected string _guid
Field Value
| Type | Description |
|---|---|
| string | Globally unique identifier for the service instance. |
_id
Declaration
protected string _id
Field Value
| Type | Description |
|---|---|
| string | Unique identifier for the service instance. |
_logService
Declaration
protected ILogService _logService
Field Value
| Type | Description |
|---|---|
| ILogService | Logging service for recording events and errors. |
_timeModeByTimerId
Declaration
protected Dictionary<string, TimeMode> _timeModeByTimerId
Field Value
| Type | Description |
|---|---|
| Dictionary<string, TimeMode> |
_timeProvider
Declaration
protected ITimeProvider _timeProvider
Field Value
| Type | Description |
|---|---|
| ITimeProvider |
_timerDictionary
Declaration
protected Dictionary<string, ITimer> _timerDictionary
Field Value
| Type | Description |
|---|---|
| Dictionary<string, ITimer> |
_timerFactory
Declaration
protected TimerFactory _timerFactory
Field Value
| Type | Description |
|---|---|
| TimerFactory |
_timerFormatterService
Declaration
protected TimerFormatterService _timerFormatterService
Field Value
| Type | Description |
|---|---|
| TimerFormatterService |
Properties
Guid
Declaration
public string Guid { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Id
Declaration
public string Id { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
LogService
Declaration
public ILogService LogService { get; set; }
Property Value
| Type | Description |
|---|---|
| ILogService |
Methods
AddTimer(string, ITimer, TimeMode, bool)
Adds an existing timer instance with the specified identifier.
Registers a pre-created timer instance with the service and sets up its parent relationship.
Declaration
public virtual void AddTimer(string id, ITimer timer, TimeMode mode = 1, bool overwrite = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | id | The unique identifier for the timer |
| ITimer | timer | The timer instance to add |
| TimeMode | mode | The time-scaling mode for the timer (default: Unscaled) |
| bool | overwrite | Whether to overwrite an existing timer with the same ID |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when a timer with the same ID already exists and overwrite is false |
AddTimer(string, TimeMode, bool)
Adds a new timer with the specified identifier, creating it using the timer factory.
Creates and configures a new timer instance with the provided ID.
Declaration
public virtual ITimer AddTimer(string id, TimeMode mode = 1, bool overwrite = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | id | The unique identifier for the new timer |
| TimeMode | mode | The time-scaling mode for the timer (default: Unscaled) |
| bool | overwrite | Whether to overwrite an existing timer with the same ID |
Returns
| Type | Description |
|---|---|
| ITimer | The newly created timer instance |
GetAllTimers()
Gets a dictionary containing all active timers managed by this service.
Provides access to the complete collection of registered timer instances.
Declaration
public virtual Dictionary<string, ITimer> GetAllTimers()
Returns
| Type | Description |
|---|---|
| Dictionary<string, ITimer> | A dictionary of timer instances keyed by their identifiers |
GetTimer(string)
Gets a timer by its unique identifier.
Searches the internal timer dictionary for the specified timer.
Declaration
public virtual ITimer GetTimer(string id)
Parameters
| Type | Name | Description |
|---|---|---|
| string | id | The unique identifier of the timer to retrieve |
Returns
| Type | Description |
|---|---|
| ITimer | The timer instance associated with the specified ID |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when no timer exists with the specified ID |
GetTimerFormatter()
Gets the timer formatter service for formatting timer values.
Provides utility methods for displaying timer information in various formats.
Declaration
public virtual TimerFormatterService GetTimerFormatter()
Returns
| Type | Description |
|---|---|
| TimerFormatterService | The timer formatter service instance |
InitializeService(TimerFactory, ILogService)
Initializes the timer service with the specified timer factory.
Sets up the factory used for creating new timer instances within this service.
Declaration
public virtual void InitializeService(TimerFactory timerFactory, ILogService logService)
Parameters
| Type | Name | Description |
|---|---|---|
| TimerFactory | timerFactory | The timer factory to use for creating new timer instances |
| ILogService | logService | The log service to be used by the timer service. |
InitializeTimeProvider(ITimeProvider)
Initializes the time provider used to read per-frame deltas.
Must be called after InitializeService(TimerFactory, ILogService) during installation.
Declaration
public void InitializeTimeProvider(ITimeProvider timeProvider)
Parameters
| Type | Name | Description |
|---|---|---|
| ITimeProvider | timeProvider | The time provider that supplies frame delta values. |
RemoveTimer(string)
Removes a timer from the service by its unique identifier.
Unregisters the timer from the internal dictionary and removes it from the service.
Declaration
public virtual void RemoveTimer(string id)
Parameters
| Type | Name | Description |
|---|---|---|
| string | id | The unique identifier of the timer to remove |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when no timer exists with the specified ID |
UpdateTimers()
Updates all registered timers for the current frame.
Reads the appropriate delta (scaled or unscaled) from the injected
ITimeProvider based on each timer's TimeMode.
Declaration
public virtual void UpdateTimers()