Local Time Scale Scripting

v1.0

Check the LocalTimeScale class to view all of the methods available.

Getting the Time Values

The LocalTimeScale will have its own time values similar to the Time class. You can get these using DeltaTime, UnscaledDeltaTime, and FixedDeltaTime.

LocalTimeScale localTimeScale;
float _counter = 0f;
float _fixedDeltaTime;

_counter += localTimeScale.DeltaTime;
_fixedCounter += localTimeScale.FixedDeltaTime;

Getting & Setting the Value

The "Value" is a float representing the percent of "real time". 1f is essentially 100% of Time.deltaTime. The Value property will return the current value, and you can also specifically set this value as well using SetValue(float newValue).

// Get the value
LocalTimeScale localTimeScale;
var currentValue = localTimeScale.Value;

// Set the value
float newValue = 0.5f;
localTimeScale.SetValue(newValue);

Pausing and Resuming

The Pause() and Resume() methods will set value to 0f, and then return the value to the last cached value. If you know the resumed value should be the same as what it was prior to the "pause" action, then this is often a simpler way of handling this logic. Otherwise, you can always just SetValue(0f) manually.

Subscribing

Any IHaveLocalTime object can subscribe to a LocalTimeScale object. This will ensure the object gets event notifications from the LocalTimeScale.

When subscribing, the OnSubscribed and OnSubscribedUnityEvent will be called.=

Information About Subscribers

Subscribers are stored in a HashSet<IHaveLocalTime> so there is no index available. There are a few helpful properties that provide more information about subscribers to a specific Local Time Scale.

Unsubscribing

You can use the UnsubscribeAll() method to call Unsubscribe() on all current subscribers.

The Unsubscribe(IHaveLocalTime subscriber) method does a few things, if the subscriber passed in is actually subscribed.

First, it will remove the subscriber from the Subscribers list, as you might expect. But it also will call subscriber.UnsubscribeFromLocalTimeScale(this), which allows the subscriber to take any actions as needed.

Next, the OnUnsubscribed and OnUnsubscribedUnityEvent will be called as well, on the Local Time Scale object. Both of these will pass the subscriber (IHaveLocalTime) object in as a parameter.

Finally, if AutoRemoveWhenEmpty is true, the time scale will be removed. See details below.

Automatically Removing the LocalTimeScale

In some cases, you may wish to automatically remove a LocalTimeScale when there are no subscribers.

Set AutoRemoveWhenEmpty to true, and then whenever subscribers Unsubscribe() from the Local Time Scale, if there are no subscribers left, then the MagicTimeManager method RemoveTimeScale() method will be called, removing this object.

The check only happens when the Unsubscribe() method is called, so you can safely set this value to true before any subscribers are active.

Subscribing to Events

Classes can subscribe to individual events on the LocalTimeScale class. This is useful to perform actions based on changes to the Local Time Scale.

In the example below, from ZoneColorChanger.cs in the demo, we subscribe and unsubscribe to events on the LocalTimeScale attached to a specific time zone.

Last updated