Skip to content

Instantly share code, notes, and snippets.

@dnovacik
Last active September 18, 2022 19:11
Show Gist options
  • Save dnovacik/2742ce5d9615ff5c49dc094a838cb2ca to your computer and use it in GitHub Desktop.
Save dnovacik/2742ce5d9615ff5c49dc094a838cb2ca to your computer and use it in GitHub Desktop.
Singleton for MonoBehaviour
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