MonoSingleton.cs 666 B

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