Last active
April 6, 2022 20:07
-
-
Save Paradox137/482cdaa37131001dcc8fdf17a408f1fc to your computer and use it in GitHub Desktop.
Singleton unity https://gamedev.stackexchange.com/questions/116009/in-unity-how-do-i-correctly-implement-the-singleton-pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Singleton<T> : MonoBehaviour where T : class | |
{ | |
private static T _instance; | |
public static T Instance { get { return _instance; } } | |
protected virtual void Awake() | |
{ | |
if (_instance != null && _instance != this as T) | |
{ | |
Destroy(this.gameObject); | |
} | |
else | |
{ | |
_instance = this as T; | |
} | |
} | |
} | |
/*public class SoundEventService : Singleton<SoundEventService> | |
{ | |
public event Action OnObstacleHit; | |
public void CallOnObstacleHit() | |
=> OnObstacleHit?.Invoke(); | |
public event Action OnButtonClick; | |
public void CallOnButtonClick() | |
=> OnButtonClick?.Invoke(); | |
protected override void Awake() | |
{ | |
base.Awake(); | |
} | |
}*/ | |
public abstract class SingletoneBase<T> : MonoBehaviour | |
where T : class | |
{ | |
/// <summary> | |
/// SingletoneBase instance back field | |
/// </summary> | |
private static T instance = null; | |
/// <summary> | |
/// SingletoneBase instance | |
/// </summary> | |
public static T Instance | |
{ | |
get | |
{ | |
if (instance == null) | |
{ | |
instance = GameObject.FindObjectOfType(typeof(T)) as T; | |
if (instance == null) | |
Debug.LogError("SingletoneBase<T>: Could not found GameObject of type " + typeof(T).Name); | |
} | |
return instance; | |
} | |
} | |
} | |
public class SomeClass : MonoBehaviour { | |
private static SomeClass _instance; | |
public static SomeClass Instance { get { return _instance; } } | |
private void Awake() | |
{ | |
if (_instance != null && _instance != this) | |
{ | |
Destroy(this.gameObject); | |
} else { | |
_instance = this; | |
} | |
} | |
} | |
public class UnitySingletonPersistent : MonoBehaviour | |
where T : Component | |
{ | |
private static T instance; | |
public static T Instance { | |
get { | |
if (instance == null) { | |
instance = FindObjectOfType<T> (); | |
if (instance == null) { | |
GameObject obj = new GameObject (); | |
obj.hideFlags = HideFlags.HideAndDontSave; | |
instance = obj.AddComponent<T> (); | |
} | |
} | |
return instance; | |
} | |
} | |
public virtual void Awake () | |
{ | |
DontDestroyOnLoad (this.gameObject); | |
if (instance == null) { | |
instance = this as T; | |
} else { | |
Destroy (gameObject); | |
} | |
} | |
} | |
public class GenericSingletonClass<T> : MonoBehaviour where T : Component | |
{ | |
private static T instance; | |
public static T Instance { | |
get { | |
if (instance == null) { | |
instance = FindObjectOfType<T> (); | |
if (instance == null) { | |
GameObject obj = new GameObject (); | |
obj.name = typeof(T).Name; | |
instance = obj.AddComponent<T>(); | |
} | |
} | |
return instance; | |
} | |
} | |
public virtual void Awake () | |
{ | |
if (instance == null) { | |
instance = this as T; | |
DontDestroyOnLoad (this.gameObject); | |
} else { | |
Destroy (gameObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment