Local Time Scale

v1.0

Creating and Using LocalTimeScale

The LocalTimeScale class is the thing that helps objects determine their final TimeScale value. There are three main ways you'll utilize Local Time Scale objects.

1️⃣ Create a Local Time Scale Scriptable Object

Right click in your project and select Create/Magic Time/Time Scale to create a new Scriptable Object.

A "Global" LocalTimeScale can be used to handle game-wide time scaling, including pausing.

Often the pre-created objects will populate the Magic Time Manager and Magic Time User lists of "Initial Time Scales".

2️⃣ Magic Time User and IUseMagicTime

The interface IHaveLocalTime is implemented by MagicTimeUser. Each Magic Time User object will automatically create its own LocalTimeScale, which you can think of as that objects internal clock.

3️⃣ Create at Runtime

LocalTimeScale objects can be created at runtime as well. Classes like the TimeZone demonstrate how this can be done.

Events & UnityEvents

Each LocalTimeScale includes UnityEvents and other events you can subscribe to.

Subscribing to Unity Events at Runtime

Since these objects are Scriptable Objects, in most cases you'll need to subscribe to the UnityEvents at runtime, specifically subscribing to the instance of the Scriptable Object that is created at runtime. Your object should reference the LocalTimeScale and add itself as a listener.

// Your script which wants to subscribe to the UnityEvent
LocalTimeScale localTimeScale;
localTimeScale.OnTimeScaleChangedUnityEvent.AddListener(OnTimeScaleChanged);

// Note the UnityEvent only provides the float value
public void OnTimeScaleChanged(float value)
{
    Debug.Log($"The new value is {value}");
}

Subscribing to the Events at Runtime

The Events also include the LocalTimeScale object, which is useful if you have one object that is subscribed to multiple LocalTimeScale objects, and needs to know which one called the event.

// Your script which wants to subscribe to the Event
LocalTimeScale localTimeScale;
localTimeScale.OnTimeScaleChanged += OnTimeScaleChanged;

// This event also tells you which object triggered it
public void OnTimeScaleChanged(float value, LocalTimeScale localTimeScale)
{
    HandleChangedTimeScaleValues(localTimeScale); // Do something!
}

Additional Scripting

Be sure to check out the Local Time Scale Scripting page with examples on how to use this class.

Local Time Scale Scripting

Last updated

Was this helpful?