Advertisement
Guest User

Untitled

a guest
Oct 11th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
  2. {
  3.  
  4. public static bool isApplicationQuitting;
  5. private static T instance;
  6. private static System.Object lockObj = new System.Object();
  7.  
  8.  
  9.  
  10. public static T Instance
  11. {
  12. get
  13. {
  14.  
  15. if (isApplicationQuitting)
  16. return null;
  17.  
  18. lock (lockObj)
  19. {
  20. if (instance == null)
  21. {
  22. instance = FindObjectOfType<T>();
  23.  
  24. if (instance == null)
  25. {
  26. var singleton = new GameObject("[SINGLETON] " + typeof(T));
  27. instance = singleton.AddComponent<T>();
  28. DontDestroyOnLoad(singleton);
  29. }
  30.  
  31. }
  32.  
  33. return instance;
  34. }
  35. }
  36. }
  37.  
  38.  
  39. public virtual void OnDestroy()
  40. {
  41. isApplicationQuitting = true;
  42. }
  43.  
  44.  
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement