Guest User

Untitled

a guest
Oct 11th, 2020
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×