Singletons

Puck makes use of singletons for all crucial systems. Two main singleton types are used within the Puck codebase:

  1. MonoBehaviorSingleton<T> - For standard Unity MonoBehaviour-based managers

  2. NetworkBehaviorSingleton<T> - For network-aware managers that need to work with Unity's Netcode for GameObjects

Accessing singleton instances is straight forward:

public class Class1 : IPuckMod
{
    public bool OnEnable()
    {
        Debug.Log($"Your global volume is {SettingsManager.Instance.GlobalVolume}!");

        SettingsManager.Instance.UpdateGlobalVolume(0.5f);
        Debug.Log($"Your global volume is now {SettingsManager.Instance.GlobalVolume}!");

        return true;
    }

    public bool OnDisable()
    {
        return true;
    }
}

Some mods will suffice with simply accessing existing public members provided by singletons themselves, though more advanced mods might require your to modify existing functionality of singletons or multi-instance classes. Let's cover how we can modify functionality with Harmony next.

Last updated