From 0e434216064fde25e84dde6b957f949ef4ecf676 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Wed, 3 May 2023 21:36:35 +0800 Subject: [PATCH] [+] BaseLogicSys [+] BaseLogicSys --- .../HotFix/GameBase/BaseLogicSys.cs | 75 +++++++ .../HotFix/GameBase/BaseLogicSys.cs.meta | 3 + .../HotFix/GameBase/BehaviourSingleton.cs | 212 ++++++++++++++++++ .../GameBase/BehaviourSingleton.cs.meta | 3 + 4 files changed, 293 insertions(+) create mode 100644 Assets/GameScripts/HotFix/GameBase/BaseLogicSys.cs create mode 100644 Assets/GameScripts/HotFix/GameBase/BaseLogicSys.cs.meta create mode 100644 Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs create mode 100644 Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs.meta diff --git a/Assets/GameScripts/HotFix/GameBase/BaseLogicSys.cs b/Assets/GameScripts/HotFix/GameBase/BaseLogicSys.cs new file mode 100644 index 00000000..4a5860be --- /dev/null +++ b/Assets/GameScripts/HotFix/GameBase/BaseLogicSys.cs @@ -0,0 +1,75 @@ +namespace TEngine +{ + /// + /// 基础LogicSys,生命周期由TEngine实现,推荐给系统实现, + /// 减少多余的Mono,保持系统层面只有一个Update。 + /// 用主Mono来驱动LogicSys的生命周期。 + /// + /// 逻辑系统类型。 + public abstract class BaseLogicSys : ILogicSys where T : new() + { + private static T _instance; + + public static bool HasInstance => _instance != null; + + public static T Instance + { + get + { + if (null == _instance) + { + _instance = new T(); + } + + return _instance; + } + } + + #region virtual function + public virtual bool OnInit() + { + if (null == _instance) + { + _instance = new T(); + } + return true; + } + + public virtual void OnStart() + { + } + + public virtual void OnUpdate() + { + } + + public virtual void OnLateUpdate() + { + } + + public virtual void OnFixedUpdate() + { + } + + public virtual void OnRoleLogout() + { + } + + public virtual void OnDestroy() + { + } + + public virtual void OnDrawGizmos() + { + } + + public virtual void OnApplicationPause(bool pause) + { + } + + public virtual void OnMapChanged() + { + } + #endregion + } +} \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameBase/BaseLogicSys.cs.meta b/Assets/GameScripts/HotFix/GameBase/BaseLogicSys.cs.meta new file mode 100644 index 00000000..c43cae84 --- /dev/null +++ b/Assets/GameScripts/HotFix/GameBase/BaseLogicSys.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fc4ce19b17fd4277951d189b66f503e2 +timeCreated: 1683120353 \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs b/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs new file mode 100644 index 00000000..812b41a8 --- /dev/null +++ b/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs @@ -0,0 +1,212 @@ +using System.Collections.Generic; + +namespace TEngine +{ + /// + /// 通过LogicSys来驱动且具备Unity完整生命周期的单例(不继承MonoBehaviour) + /// + /// + public abstract class BehaviourSingleton : BaseBehaviourSingleton where T : BaseBehaviourSingleton, new() + { + private static T _instance; + + public static T Instance + { + get + { + if (null == _instance) + { + _instance = new T(); + Log.Assert(_instance != null); + _instance.Awake(); + RegSingleton(_instance); + } + + return _instance; + } + } + + private static void RegSingleton(BaseBehaviourSingleton inst) + { + BehaviourSingleSystem.Instance.RegSingleton(inst); + } + } + + public class BaseBehaviourSingleton + { + public bool IsStart = false; + + public virtual void Active() + { + } + + public virtual void Awake() + { + } + + public virtual bool IsHaveLateUpdate() + { + return false; + } + + public virtual void Start() + { + } + + public virtual void Update() + { + } + + public virtual void LateUpdate() + { + } + + public virtual void Destroy() + { + } + + public virtual void OnPause() + { + } + + public virtual void OnResume() + { + } + + public virtual void OnDrawGizmos() + { + } + } + + public class BehaviourSingleSystem : BaseLogicSys + { + private readonly List _listInst = new List(); + private readonly List _listStart = new List(); + private readonly List _listUpdate = new List(); + private readonly List _listLateUpdate = new List(); + + public void RegSingleton(BaseBehaviourSingleton inst) + { + Log.Assert(!_listInst.Contains(inst)); + _listInst.Add(inst); + _listStart.Add(inst); + } + + public void UnRegSingleton(BaseBehaviourSingleton inst) + { + if (inst == null) + { + Log.Error($"BaseBehaviourSingleton Is Null"); + return; + } + + Log.Assert(_listInst.Contains(inst)); + if (_listInst.Contains(inst)) + { + _listInst.Remove(inst); + } + + if (_listStart.Contains(inst)) + { + _listStart.Remove(inst); + } + + if (_listUpdate.Contains(inst)) + { + _listUpdate.Remove(inst); + } + + if (_listLateUpdate.Contains(inst)) + { + _listLateUpdate.Remove(inst); + } + + inst.Destroy(); + inst = null; + } + + public override void OnUpdate() + { + var listStart = _listStart; + var listToUpdate = _listUpdate; + var listToLateUpdate = _listLateUpdate; + if (listStart.Count > 0) + { + for (int i = 0; i < listStart.Count; i++) + { + var inst = listStart[i]; + Log.Assert(!inst.IsStart); + + inst.IsStart = true; + inst.Start(); + listToUpdate.Add(inst); + + if (inst.IsHaveLateUpdate()) + { + listToLateUpdate.Add(inst); + } + } + + listStart.Clear(); + } + + var listUpdateCnt = listToUpdate.Count; + for (int i = 0; i < listUpdateCnt; i++) + { + var inst = listToUpdate[i]; + + TProfiler.BeginFirstSample(inst.GetType().FullName); + inst.Update(); + TProfiler.EndFirstSample(); + } + } + + public override void OnLateUpdate() + { + var listLateUpdate = _listLateUpdate; + var listLateUpdateCnt = listLateUpdate.Count; + for (int i = 0; i < listLateUpdateCnt; i++) + { + var inst = listLateUpdate[i]; + + TProfiler.BeginFirstSample(inst.GetType().FullName); + inst.LateUpdate(); + TProfiler.EndFirstSample(); + } + } + + public override void OnDestroy() + { + for (int i = 0; i < _listInst.Count; i++) + { + var inst = _listInst[i]; + inst.Destroy(); + } + } + + public override void OnApplicationPause(bool pause) + { + for (int i = 0; i < _listInst.Count; i++) + { + var inst = _listInst[i]; + if (pause) + { + inst.OnPause(); + } + else + { + inst.OnResume(); + } + } + } + + public override void OnDrawGizmos() + { + for (int i = 0; i < _listInst.Count; i++) + { + var inst = _listInst[i]; + inst.OnDrawGizmos(); + } + } + } +} \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs.meta b/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs.meta new file mode 100644 index 00000000..4d688387 --- /dev/null +++ b/Assets/GameScripts/HotFix/GameBase/BehaviourSingleton.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5c0e9c1c8c9d4ce99a1c991fb62a0256 +timeCreated: 1683120460 \ No newline at end of file