Skip to content

Instantly share code, notes, and snippets.

@inertiave
Created October 2, 2019 13:29
Show Gist options
  • Save inertiave/49b011d8c9aba40a982ddfeefe032f2e to your computer and use it in GitHub Desktop.
Save inertiave/49b011d8c9aba40a982ddfeefe032f2e to your computer and use it in GitHub Desktop.
using UnityEngine;
public class SingletonMonoBehaviour<T> : MonoBehaviour where T : SingletonMonoBehaviour<T> {
static T _inst;
public static T Instance {
get {
if (_inst == null) {
_inst = GameObject.FindObjectOfType(typeof(T)) as T;
if (_inst == null) {
GameObject singleton = new GameObject();
_inst = singleton.AddComponent<T>();
_inst.name = $"[{_inst.GetType().Name}]";
}
}
return _inst;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment