Singleton.cs 748 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #if !NOT_UNITY
  2. using UnityEngine;
  3. public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
  4. {
  5. protected static T _instance;
  6. private static bool missed = false;
  7. public static T instance
  8. {
  9. get
  10. {
  11. if (!_instance)// && (!Application.isPlaying || !missed))
  12. {
  13. _instance = Object.FindObjectOfType<T>(true);
  14. if (!_instance)
  15. {
  16. missed = true;
  17. }
  18. }
  19. return _instance;
  20. }
  21. }
  22. protected virtual void Awake()
  23. {
  24. _instance = this as T;
  25. missed = false;
  26. }
  27. private void OnDestroy()
  28. {
  29. _instance = null;
  30. missed = false;
  31. }
  32. }
  33. #endif