Skip to content

Singleton

Wagner GFX edited this page Dec 5, 2024 · 1 revision

This plugin contains two singleton base classes, one Monobehaviour Component and one Scriptable Object Class.

These classes are not made to create ease of access through static fields, instead, they are designed to only allow one object of its type in existence.

The generic type of the Instance static property allows for different inherited classes to be evaluated separately.

Plugin Classes

MonoSingletonBehaviour

A Component that inherits from this class will destroy its own GameObject during runtime when detecting a duplicate.

This Singleton behavior allows for any scene to contain a type of GameManager prefab that will instantiate itself and create a base environment to test individual components.
On the other hand, if it detects an existing instance, it will delete itself to prevent duplication.

Any other Components in the scene that searches for a GameObject with a singleton component will find only one instance.

ScriptableSingletonObject

A Scriptable Object that inherits from this class will throw a warning in the console when detecting more than one asset file in existence.

This difference from the Component Singleton is necessary to prevent the dangerous and unwanted behavior of automatically deleting asset files from the project.

Clone this wiki locally