SingletonBase.cs 523 B

1234567891011121314151617181920212223
  1. using System;
  2. using UnityEngine;
  3. public class SingletonBase<T> : MonoBehaviour where T : MonoBehaviour
  4. {
  5. public static T Instance
  6. {
  7. get
  8. {
  9. if (SingletonBase<T>.instance == null)
  10. {
  11. SingletonBase<T>.instance = (T)((object)UnityEngine.Object.FindObjectOfType(typeof(T)));
  12. if (SingletonBase<T>.instance == null)
  13. {
  14. Debug.LogError("An instance of " + typeof(T) + " is needed in the scene, but there is none.");
  15. }
  16. }
  17. return SingletonBase<T>.instance;
  18. }
  19. }
  20. private static T instance;
  21. }