Created
October 2, 2019 13:29
-
-
Save inertiave/49b011d8c9aba40a982ddfeefe032f2e to your computer and use it in GitHub Desktop.
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
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