Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement