Decorate a MonoBehaviour or ScriptableObject class with this
attribute to register it as an automatically managed singleton. The editor will create and
maintain the asset or prefab, and the runtime will instantiate it at startup.
Public Fields
| string displayName |
Name given to this singleton in the Assets folder. |
| string folderPath |
Where to create the asset or prefab, relative to the Assets folder. When inherited is true, derived classes are also created here. |
| bool inherited |
When true, the attribute also applies to classes derived from the decorated type. |
Utility to manually add and remove singleton instances at runtime. Use this when you need
to register objects that are not covered by SingletonAttribute
and any reference type is accepted. All methods require play mode.
Adding a MonoBehaviour via Add does not reparent it under the
Auto Singleton root GameObject, so it will be destroyed on scene load unless you call
Object.DontDestroyOnLoad on it yourself.
Static Methods: Add
| void Add<T>(T singleton) where T : class |
Register a singleton instance. Throws ArgumentException if an instance of the same exact type is already registered. |
| void Add<T>(IEnumerable<T> singletons) where T : class |
Register multiple singleton instances. Throws ArgumentNullException if the collection is null. |
| void Add<T>(params T[] singletons) where T : class |
Register multiple singleton instances via params. |
Static Methods: Remove
Only instances that were manually added via Add can be removed.
Automatically managed singletons cannot be removed at runtime.
| void Remove<T>(T singleton) where T : class |
Unregister a singleton instance. Throws ArgumentException if the instance is not registered or was not added manually. |
| void Remove<T>(IEnumerable<T> singletons) where T : class |
Unregister multiple singleton instances. Throws ArgumentNullException if the collection is null. |
| void Remove<T>(params T[] singletons) where T : class |
Unregister multiple singleton instances via params. |
Generic access class for singleton instances. T can be a concrete type, a
base class, or an interface. Instances returns every registered object
assignable to T, while Instance returns the one selected by a
SelectInstance call (or the only one if there is exactly one registered
instance of exact type T). All members require play mode; in the editor,
accessing them outside play mode throws InvalidOperationException.
Constraint: T : class.
Static Properties
| T Instance |
The selected singleton instance. In the editor, throws InvalidOperationException if no instance is registered or if multiple are registered and none was selected via SelectInstance. Returns null in builds when no instance is available. |
| IReadOnlyList<T> Instances |
All registered singleton instances assignable to T, regardless of which one is selected. |
| bool HasInstance |
True if Instance has a value. Use this to check before accessing Instance to avoid an exception. |
Static Methods: Access
| bool TryGetInstance(out T instance) |
Non-throwing alternative to Instance. Sets instance to the selected singleton and returns true; sets it to null and returns false if none is selected. |
| T[] Find(Predicate<T> predicate) |
Return a new array of all registered instances that match the predicate. Returns an empty array if none match. |
Static Methods: SelectInstance
All SelectInstance overloads set Instance to a chosen entry
from Instances and return true on success. On failure they
leave Instance unchanged, log a warning in the editor, and return
false.
| bool SelectInstance(Predicate<T> predicate) |
Select the unique instance matching the predicate. Fails if zero or more than one instance matches. |
| bool SelectInstance(Func<T, int> priority) |
Select the instance with the highest int priority score. Fails if multiple instances share the top score. |
| bool SelectInstance(Func<T, float> priority) |
Select the instance with the highest float priority score. Fails if multiple instances share the top score. |
| bool SelectInstance(T instance) |
Select the given instance directly. Fails if it is not in the registered collection. |
| bool SelectInstance<SubT>() where SubT : T |
Select the registered instance whose exact runtime type is SubT. Fails if none is found. |
| bool SelectInstance() |
Select the sole registered instance derived from T. Fails if there are zero or more than one. |