Table of Contents

Class Singleton<T>

Namespace
Gamelogic.Extensions
Assembly
Gamelogic.Extensions.dll

Provides a generic implementation of a singleton-like pattern for MonoBehaviour classes. This class automatically searches for an existing instance in the scene or logs an error if none or more than one are found.

[Version(1, 0, 0)]
public class Singleton<T> : GLMonoBehaviour where T : MonoBehaviour

Type Parameters

T

The type of the Singleton class derived from MonoBehaviour.

Inheritance
Singleton<T>
Derived
Inherited Members
MonoBehaviour.IsInvoking()
MonoBehaviour.StopCoroutine(Coroutine)
MonoBehaviour.StopAllCoroutines()
MonoBehaviour.runInEditMode
Component.GetComponent<T>()
Component.TryGetComponent<T>(out T)
Component.GetComponentInChildren<T>()
Component.GetComponentsInChildren<T>()
Component.GetComponentInParent<T>()
Component.GetComponents<T>()
Component.gameObject
Object.Instantiate(Object, Vector3, Quaternion, Transform)
Object.Instantiate(Object)
Object.Instantiate(Object, Transform)
Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
Object.Instantiate<T>(T, Transform)
Object.Destroy(Object)
Object.DestroyImmediate(Object)
Object.DestroyObject(Object)
Object.ToString()
Object.hideFlags
Extension Methods

Remarks

Singletons usually manage their own creation, but it is common in Unity projects to have a singleton already placed in the scene. In this sense, this class really checks whether an instance can be used as a singleton, and then provides access to the instance.

If you want to create a singleton that is not already placed in the scene, you can use put
<xref href="Gamelogic.Extensions.SingletonSpawner%601" data-throw-if-not-resolved="false"></xref> in the scene instead.

Fields

instance

Properties

HasInstanceInOpenScenes

Gets a value indicating whether this singleton has a unique instance in open scenes.

Instance

Returns the instance of this singleton.

IsReady

Gets a value indicating whether this singleton is ready, that is, a unique instance is available in the scene.

Methods

FindAndConnectInstance()

Finds an instance of the given type in the open scenes and assign it to the instance field of the Singleton<T>.

OnDestroy()