123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using System;
- using UnityEngine;
- public abstract class ControllerProxyBase<T> : BaseBehaviour where T : BaseBehaviour
- {
- [HideInInspector]
- private protected T effect { get; private set; }
- private void Start()
- {
- this.ResetEffect();
- this.ResetToDefault();
- this.ThisStart();
- }
- private void OnEnable()
- {
- ControllerProxyBase<T>.effectLock = this;
- this.ResetEffect();
- this.OnThisEnable();
- }
- private void ResetEffect()
- {
- if (this.effect == null)
- {
- if (Camera.main != null)
- {
- this.effect = this.GetEffect();
- if (this.effect != null && this._autoEnable)
- {
- T effect = this.effect;
- effect.enabled = true;
- }
- }
- else
- {
- UnityEngine.Debug.Log("Can not find Camera");
- }
- }
- }
- private void Update()
- {
- if (object.ReferenceEquals(ControllerProxyBase<T>.effectLock, this) && this.effect != null)
- {
- T effect = this.effect;
- if (effect.gameObject.activeSelf)
- {
- this.ThisUpdate();
- }
- }
- }
- private void OnDisable()
- {
- this.ResetToDefault();
- this.ThisUpdate();
- this.OnThisDisable();
- if (object.ReferenceEquals(ControllerProxyBase<T>.effectLock, this))
- {
- ControllerProxyBase<T>.effectLock = null;
- if (this.effect != null && this._autoEnable)
- {
- T effect = this.effect;
- effect.enabled = false;
- this.effect = (T)((object)null);
- }
- }
- }
- protected virtual T GetEffect()
- {
- if (typeof(T).IsSubclassOf(typeof(SingletonMono<T>)))
- {
- return SingletonMono<T>.Instance;
- }
- T component = Camera.main.GetComponent<T>();
- if (component != null)
- {
- return component;
- }
- return UnityEngine.Object.FindObjectOfType<T>();
- }
- protected virtual void ThisUpdate()
- {
- }
- protected virtual void ResetToDefault()
- {
- }
- protected virtual void ThisStart()
- {
- }
- protected virtual void OnThisEnable()
- {
- }
- protected virtual void OnThisDisable()
- {
- }
- protected static object effectLock;
- protected bool _autoEnable = true;
- }
|