Last active
September 18, 2022 19:11
-
-
Save dnovacik/2742ce5d9615ff5c49dc094a838cb2ca to your computer and use it in GitHub Desktop.
Singleton for MonoBehaviour
This file contains 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 SingletonPattern<T> : MonoBehaviour where T : SingletonPattern<T> | |
{ | |
public static T Instance; | |
protected virtual void Awake() | |
{ | |
if (Instance != null) | |
{ | |
Destroy(this); | |
} | |
else | |
{ | |
Instance = this as T; | |
} | |
} | |
} | |
// then in some singleton/manager class | |
public class UIManager : SingletonPattern<UIManager> | |
{ | |
protected override void Awake() | |
{ | |
base.Awake(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment