MonoSingleton.cs 617 B

123456789101112131415161718192021222324252627282930313233
  1. using UnityEngine;
  2. public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
  3. {
  4. public bool global = true;
  5. static T instance;
  6. protected bool _isInit = false;
  7. public static T Instance
  8. {
  9. get
  10. {
  11. if (instance == null)
  12. {
  13. instance =(T)FindObjectOfType<T>();
  14. }
  15. return instance;
  16. }
  17. }
  18. void Start()
  19. {
  20. if (global) DontDestroyOnLoad(this.gameObject);
  21. //Debug.Log(this.gameObject.name);
  22. this.OnStart();
  23. }
  24. protected virtual void OnStart()
  25. {
  26. }
  27. }