Class used to get game subsystems, using generics and then making cache of said subsystems.
The SubSystems class is used to easily find any subsystem that has been registered on it.
It helps us avoid falling into the Singleton pattern, and does something similar to the Service Locator pattern. Also good for performance reasons, It can handle over a million calls every frame with no issues.
Using the SubSystems class
To register a new subsystem you can call the SubSystems.Register(this) method, passing in a class that extends from SubSystem or NetworkSubSystem, it'll add that subsystem into a dictionary of subsystems, preventing two of the same subsystem from existing.
Then you can get this subsystem from somewhere else using SubSystems.Get<T>()
⚠️** **IMPORTANT!
Note that is done automatically by classes inheriting SubSystem and NetworkedSubSystem. They both register on Awake and unregister on Destroy:
public class SubSystem : Actor
{
protected override void OnAwake()
{
base.OnAwake();
SubSystemLocator.Register(this);
}
protected override void OnDestroyed()
{
base.OnDestroyed();
SubSystemLocator.Unregister(this);
}
}