Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class SingletonMonoBehaviour<T> : MonoBehaviour where T : SingletonMonoBehaviour<T> {
  4.  
  5. static T _inst;
  6.  
  7. public static T Instance {
  8. get {
  9. if (_inst == null) {
  10. _inst = GameObject.FindObjectOfType(typeof(T)) as T;
  11. if (_inst == null) {
  12. GameObject singleton = new GameObject();
  13. _inst = singleton.AddComponent<T>();
  14. _inst.name = $"[{_inst.GetType().Name}]";
  15. }
  16. }
  17.  
  18. return _inst;
  19. }
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement