ControllerProxyBase`1.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using UnityEngine;
  3. public abstract class ControllerProxyBase<T> : BaseBehaviour where T : BaseBehaviour
  4. {
  5. [HideInInspector]
  6. private protected T effect { get; private set; }
  7. private void Start()
  8. {
  9. this.ResetEffect();
  10. this.ResetToDefault();
  11. this.ThisStart();
  12. }
  13. private void OnEnable()
  14. {
  15. ControllerProxyBase<T>.effectLock = this;
  16. this.ResetEffect();
  17. this.OnThisEnable();
  18. }
  19. private void ResetEffect()
  20. {
  21. if (this.effect == null)
  22. {
  23. if (Camera.main != null)
  24. {
  25. this.effect = this.GetEffect();
  26. if (this.effect != null && this._autoEnable)
  27. {
  28. T effect = this.effect;
  29. effect.enabled = true;
  30. }
  31. }
  32. else
  33. {
  34. UnityEngine.Debug.Log("Can not find Camera");
  35. }
  36. }
  37. }
  38. private void Update()
  39. {
  40. if (object.ReferenceEquals(ControllerProxyBase<T>.effectLock, this) && this.effect != null)
  41. {
  42. T effect = this.effect;
  43. if (effect.gameObject.activeSelf)
  44. {
  45. this.ThisUpdate();
  46. }
  47. }
  48. }
  49. private void OnDisable()
  50. {
  51. this.ResetToDefault();
  52. this.ThisUpdate();
  53. this.OnThisDisable();
  54. if (object.ReferenceEquals(ControllerProxyBase<T>.effectLock, this))
  55. {
  56. ControllerProxyBase<T>.effectLock = null;
  57. if (this.effect != null && this._autoEnable)
  58. {
  59. T effect = this.effect;
  60. effect.enabled = false;
  61. this.effect = (T)((object)null);
  62. }
  63. }
  64. }
  65. protected virtual T GetEffect()
  66. {
  67. if (typeof(T).IsSubclassOf(typeof(SingletonMono<T>)))
  68. {
  69. return SingletonMono<T>.Instance;
  70. }
  71. T component = Camera.main.GetComponent<T>();
  72. if (component != null)
  73. {
  74. return component;
  75. }
  76. return UnityEngine.Object.FindObjectOfType<T>();
  77. }
  78. protected virtual void ThisUpdate()
  79. {
  80. }
  81. protected virtual void ResetToDefault()
  82. {
  83. }
  84. protected virtual void ThisStart()
  85. {
  86. }
  87. protected virtual void OnThisEnable()
  88. {
  89. }
  90. protected virtual void OnThisDisable()
  91. {
  92. }
  93. protected static object effectLock;
  94. protected bool _autoEnable = true;
  95. }