using CG.Framework; using System.Collections; using UnityEngine; using UnityEngine.Events; /******************************************************************************* *Create By CG *Function 让不继承MonoBehaviour类可以开启协程,可以用FixedUpdate、Update、LateUpdate方法更新 *******************************************************************************/ namespace CG.UTility { public class MonoManager : ClassSingleton { //构造方法私有化,防止外部new对象。 private MonoManager() { } //执行Mono逻辑的属性。 private MonoController monoExecuter; private MonoController MonoExecuter { get { //会确保场景中有一个游戏对象挂载了MonoController脚本。如果没有,则会自动创建该游戏对象,并挂载MonoController脚本。 if (monoExecuter == null) { GameObject go = new GameObject(typeof(MonoController).Name); monoExecuter = go.AddComponent(); } return monoExecuter; } } /// /// 添加FixedUpdate的事件。 /// public void AddFixedUpdateListener(UnityAction call) { MonoExecuter.AddFixedUpdateListener(call); } /// /// 移除FixedUpdate的事件。 /// public void RemoveFixedUpdateListener(UnityAction call) { MonoExecuter.RemoveFixedUpdateListener(call); } /// /// 移除所有FixedUpdate的事件。 /// public void RemoveAllFixedUpdateListeners() { MonoExecuter.RemoveAllFixedUpdateListeners(); } /// /// 添加Update的事件。 /// public void AddUpdateListener(UnityAction call) { MonoExecuter.AddUpdateListener(call); } /// /// 移除Update的事件。 /// public void RemoveUpdateListener(UnityAction call) { MonoExecuter.RemoveUpdateListener(call); } /// /// 移除所有Update的事件。 /// public void RemoveAllUpdateListeners() { MonoExecuter.RemoveAllUpdateListeners(); } /// /// 添加LateUpdate的事件。 /// public void AddLateUpdateListener(UnityAction call) { MonoExecuter.AddLateUpdateListener(call); } /// /// 移除LateUpdate的事件。 /// public void RemoveLateUpdateListener(UnityAction call) { MonoExecuter.RemoveLateUpdateListener(call); } /// /// 移除所有LateUpdate的事件。 /// public void RemoveAllLateUpdateListeners() { MonoExecuter.RemoveAllLateUpdateListeners(); } /// /// 移除所有Update、FixedUpdate、LateUpdate的事件。 /// public void RemoveAllListeners() { MonoExecuter.RemoveAllListeners(); } /// /// 开启协程、停止协程。 /// public Coroutine StartCoroutine(IEnumerator routine) { return MonoExecuter.StartCoroutine(routine); } public void StopCoroutine(IEnumerator routine) { if (routine != null) MonoExecuter.StopCoroutine(routine); } public void StopCoroutine(Coroutine routine) { if (routine != null) MonoExecuter.StopCoroutine(routine); } public void StopAllCoroutines() { MonoExecuter.StopAllCoroutines(); } public void DestroyImmediate(Object obj) { MonoExecuter.DestroyImmediateWithCheck(obj); } //用于执行MonoBehaviour逻辑的脚本。会挂载到场景的游戏对象MonoExecuter身上。 private class MonoController : MonoBehaviour { event UnityAction updateEvent;//在生命周期方法Update中执行的事件。 event UnityAction fixedUpdaetEvent;//在生命周期方法FixedUpdate中执行的事件。 event UnityAction lateUpdateEvent;//在生命周期方法LateUpdate中执行的事件。 void Awake() { DontDestroyOnLoad(gameObject); } void FixedUpdate() { fixedUpdaetEvent?.Invoke(); } void Update() { updateEvent?.Invoke(); } void LateUpdate() { lateUpdateEvent?.Invoke(); } /// /// 添加FixedUpdate的事件。 /// /// 要监听的事件 public void AddFixedUpdateListener(UnityAction call) { fixedUpdaetEvent += call; } /// /// 移除FixedUpdate的事件。 /// /// 要监听的事件 public void RemoveFixedUpdateListener(UnityAction call) { fixedUpdaetEvent -= call; } /// /// 移除所有FixedUpdate的事件。 /// public void RemoveAllFixedUpdateListeners() { fixedUpdaetEvent = null; } /// /// 添加Update的事件。 /// /// 要监听的事件 public void AddUpdateListener(UnityAction call) { updateEvent += call; } /// /// 移除Update的事件。 /// public void RemoveUpdateListener(UnityAction call) { updateEvent -= call; } /// /// 移除所有Update的事件。 /// public void RemoveAllUpdateListeners() { updateEvent = null; } /// /// 添加LateUpdate的事件。 /// 要监听的事件 /// public void AddLateUpdateListener(UnityAction call) { lateUpdateEvent += call; } /// /// 移除LateUpdate的事件。 /// 要监听的事件 /// public void RemoveLateUpdateListener(UnityAction call) { lateUpdateEvent -= call; } /// /// 移除所有LateUpdate的事件。 /// public void RemoveAllLateUpdateListeners() { lateUpdateEvent = null; } /// /// 移除所有FixedUpdate、Update、LateUpdate的事件。 /// public void RemoveAllListeners() { RemoveAllFixedUpdateListeners(); RemoveAllUpdateListeners(); RemoveAllUpdateListeners(); } public void DestroyImmediateWithCheck(Object obj) { if (Application.isEditor) { DestroyImmediate(obj); } else { Destroy(obj); } } } } }