MonoSingleton.cs 740 B

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