25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

52 lines
1.2 KiB

  1. using System;
  2. using UnityEngine;
  3. namespace SUISS.Core
  4. {
  5. public abstract class StaticMonoBehaviour<T> : MonoBehaviour where T : StaticMonoBehaviour<T>
  6. {
  7. public static bool IsAvailable
  8. {
  9. get
  10. {
  11. return !object.ReferenceEquals(StaticMonoBehaviour<T>._instance, null);
  12. }
  13. }
  14. public static T Instance
  15. {
  16. get
  17. {
  18. if (object.ReferenceEquals(StaticMonoBehaviour<T>._instance, null))
  19. {
  20. UnityEngine.Debug.LogWarning("Could not find an instance of " + typeof(T).Name + ". Make sure it is in the scene and to give it time for its Awake() to be called.");
  21. return (T)((object)null);
  22. }
  23. return StaticMonoBehaviour<T>._instance;
  24. }
  25. }
  26. protected virtual void Awake()
  27. {
  28. if (object.ReferenceEquals(StaticMonoBehaviour<T>._instance, null))
  29. {
  30. StaticMonoBehaviour<T>._instance = (T)((object)this);
  31. }
  32. else
  33. {
  34. UnityEngine.Debug.LogError("Found multiple instances of " + typeof(T).Name + " in the scene.");
  35. }
  36. }
  37. protected virtual void OnDestroy()
  38. {
  39. if (object.ReferenceEquals(StaticMonoBehaviour<T>._instance, this))
  40. {
  41. StaticMonoBehaviour<T>._instance = (T)((object)null);
  42. }
  43. }
  44. private static T _instance;
  45. }
  46. }