From 036c89a6d197ecfe4ced58bc83972dd021d5367a Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Sat, 13 May 2023 20:35:34 +0800 Subject: [PATCH] Update BehaviourSingleton.cs --- .../HotFix/GameBase/BehaviourSingleton.cs | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs b/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs index 812b41a8..81d3e59b 100644 --- a/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs +++ b/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs @@ -1,9 +1,29 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace TEngine { /// - /// 通过LogicSys来驱动且具备Unity完整生命周期的单例(不继承MonoBehaviour) + /// 帧更新标签。 + /// + [AttributeUsage(AttributeTargets.Class)] + public class UpdateAttribute : Attribute { } + + /// + /// 后帧更新标签。 + /// + [AttributeUsage(AttributeTargets.Class)] + public class LateUpdateAttribute : Attribute{ } + + /// + /// 物理帧更新标签。 + /// + [AttributeUsage(AttributeTargets.Class)] + public class FixedUpdateAttribute : Attribute{ } + + + /// + /// 通过LogicSys来驱动且具备Unity完整生命周期的单例(不继承MonoBehaviour)。 /// /// public abstract class BehaviourSingleton : BaseBehaviourSingleton where T : BaseBehaviourSingleton, new() @@ -53,13 +73,29 @@ namespace TEngine { } + /// + /// 帧更新。 + /// 需要UpdateAttribute。 + /// public virtual void Update() { } + /// + /// 后帧更新。 + /// 需要LateUpdateAttribute。 + /// public virtual void LateUpdate() { } + + /// + /// 物理帧更新。 + /// 需要FixedUpdateAttribute。 + /// + public virtual void FixedUpdate() + { + } public virtual void Destroy() { @@ -84,12 +120,25 @@ namespace TEngine private readonly List _listStart = new List(); private readonly List _listUpdate = new List(); private readonly List _listLateUpdate = new List(); + private readonly List _listFixedUpdate = new List(); public void RegSingleton(BaseBehaviourSingleton inst) { Log.Assert(!_listInst.Contains(inst)); _listInst.Add(inst); _listStart.Add(inst); + if (HadAttribute(inst.GetType())) + { + _listUpdate.Add(inst); + } + if (HadAttribute(inst.GetType())) + { + _listLateUpdate.Add(inst); + } + if (HadAttribute(inst.GetType())) + { + _listFixedUpdate.Add(inst); + } } public void UnRegSingleton(BaseBehaviourSingleton inst) @@ -174,6 +223,20 @@ namespace TEngine TProfiler.EndFirstSample(); } } + + public override void OnFixedUpdate() + { + var listFixedUpdate = _listFixedUpdate; + var listFixedUpdateCnt = listFixedUpdate.Count; + for (int i = 0; i < listFixedUpdateCnt; i++) + { + var inst = listFixedUpdate[i]; + + TProfiler.BeginFirstSample(inst.GetType().FullName); + inst.FixedUpdate(); + TProfiler.EndFirstSample(); + } + } public override void OnDestroy() { @@ -208,5 +271,12 @@ namespace TEngine inst.OnDrawGizmos(); } } + + private bool HadAttribute(Type type) where T:Attribute + { + T attribute = Attribute.GetCustomAttribute(type, typeof(T)) as T; + + return attribute != null; + } } } \ No newline at end of file