SingletonMonobehaviour.cs 641 B

1234567891011121314151617181920212223242526272829
  1. using UnityEngine;
  2. namespace Blue
  3. {
  4. public class SingletonMonobehaviour<T> : AbstractController where T:SingletonMonobehaviour<T>
  5. {
  6. private static T _instance;
  7. public static T Instance
  8. {
  9. get => _instance;
  10. }
  11. public virtual void OnAwake() { }
  12. private void Awake()
  13. {
  14. if (_instance == null)
  15. {
  16. _instance = this.GetComponent<T>();
  17. _instance.OnAwake();
  18. DontDestroyOnLoad(_instance);
  19. }
  20. else
  21. {
  22. Destroy(gameObject);
  23. }
  24. }
  25. }
  26. }