UxrAbstractSingleton<T> Class
A singleton base class that can be used with abstract classes.
The difference with UxrSingleton<T> is that UxrSingleton<T> guarantees that an instance will always be available in the scene by instantiating the component if it’s not found. This means Instance will always be non-null and can be used with or without an instance available in the scene. UxrSingleton<T> also allows to use automatic prefab instantiation if a compatible singleton prefab is present in a special Resources folder. Since abstract classes can’t be instantiated, Instance in UxrAbstractSingleton<T> will be non-null only if a child component is available somewhere in the scene.
For design purposes, a singleton may still be desirable when programming an abstract class, hence this UxrAbstractSingleton<T> component base class.
Inheritance Hierarchy
System.Object
Object
Component
Behaviour
MonoBehaviour
UltimateXR.Core.Components.UxrComponent
UltimateXR.Core.Components.Singleton.UxrAbstractSingleton<T>
UltimateXR.Core.Components.Singleton.UxrAsyncInitAbstractSingleton<T>
UltimateXR.Core.Components.Singleton.UxrSingleton<T>
Namespace: UltimateXR.Core.Components.Singleton
Assembly: UltimateXR (in UltimateXR.dll) Version: 0.0.0.0
Syntax
C#
public abstract class UxrAbstractSingleton<T> : UxrComponent
where T : UxrAbstractSingleton<T>
Type Parameters
- T
- Class the singleton is for
Constructors
Name | Description | |
---|---|---|
UxrAbstractSingleton<T> |
Properties
Name | Description | |
---|---|---|
HasInstance | Gets whether there is a singleton instance available. | |
Instance | Gets the unique, global instance of the given component. | |
IsInitialized | Gets or sets whether the singleton has been initialized. | |
NeedsDontDestroyOnLoad | Whether the singleton requires DontDestroyOnLoad(Object) applied to the GameObject so that it doesn’t get destroyed when a new scene is loaded. |
Methods
Name | Description | |
---|---|---|
Awake | Tries to set the singleton instance. (Overrides UxrComponent.Awake().) | |
GetInstance | Gets the singleton instance. | |
InitInternal | The default internal initialization. Child classes can override this method if they require initialization code. | |
OnDestroy | Destroys the singleton instance. (Overrides UxrComponent.OnDestroy().) | |
ReleaseInternal | The default internal release. Child classes can override this method if they required deallocation code. | |
TrySetInstance | Tries to set the singleton instance. |
Extension Methods
Name | Description | |
---|---|---|
CheckSetEnabled | Enables/disabled the component if it isn’t enabled already. (Defined by MonoBehaviourExt.) | |
GetOrAddComponent<T> | Gets the Component of a given type. If it doesn’t exist, it is added to the GameObject. (Defined by ComponentExt.) | |
GetPathUnderScene | Gets the full path under current scene, including all parents, but scene name, for the given component. (Defined by ComponentExt.) | |
GetSceneUid | Gets an unique identifier string for the given component. (Defined by ComponentExt.) | |
GetUniqueScenePath | Gets an unique path in the scene for the given component. It will include scene name, sibling and component indices to make it unique. (Defined by ComponentExt.) | |
LoopCoroutine | Creates a coroutine that simplifies executing a loop during a certain amount of time. (Defined by MonoBehaviourExt.) | |
SafeGetComponentInParent<T> | Gets the Component of a given type in the GameObject or any of its parents. It also works on prefabs, where regular GetComponentInParent(Type, Boolean) will not work: https://issuetracker.unity3d.com/issues/getcomponentinparent-is-returning-null-when-the-gameobject-is-a-prefab (Defined by ComponentExt.) | |
ShowInInspector(Boolean) | Overloaded. Controls whether to show the current object in the inspector. (Defined by ObjectExt.) | |
ShowInInspector(Boolean, Boolean) | Overloaded. Controls whether to show the current object in the inspector and whether it is editable. (Defined by ObjectExt.) | |
ThrowIfNull | Throws an exception if the object is null. (Defined by ObjectExt.) |
Remarks
- Make sure to call base.Awake() first in child classes where Awake() is used.
- Use HasInstance to check whether the instance exists.