From 923073f410ec5f9ce2ca7c7711fa4ca6bc474ddf Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Wed, 8 Jun 2022 10:30:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86BehaviorSingleton?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了BehaviorSingleton --- .../Runtime/Core/BehaviourSingleton.cs | 165 ++++++++++++++++++ .../Runtime/Core/BehaviourSingleton.cs.meta | 11 ++ Assets/TEngine/Runtime/Core/TLogger.cs | 8 +- Assets/TEngine/Runtime/Core/TProfiler.cs | 67 +++++++ Assets/TEngine/Runtime/Core/TProfiler.cs.meta | 11 ++ Assets/TEngine/Runtime/UI/Demo/TEngineTest.cs | 1 + 6 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 Assets/TEngine/Runtime/Core/BehaviourSingleton.cs create mode 100644 Assets/TEngine/Runtime/Core/BehaviourSingleton.cs.meta create mode 100644 Assets/TEngine/Runtime/Core/TProfiler.cs create mode 100644 Assets/TEngine/Runtime/Core/TProfiler.cs.meta diff --git a/Assets/TEngine/Runtime/Core/BehaviourSingleton.cs b/Assets/TEngine/Runtime/Core/BehaviourSingleton.cs new file mode 100644 index 00000000..8c34594e --- /dev/null +++ b/Assets/TEngine/Runtime/Core/BehaviourSingleton.cs @@ -0,0 +1,165 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TEngine +{ + public class BehaviourSingleton : BaseBehaviourSingleton where T : BaseBehaviourSingleton, new() + { + private static T sInstance; + public static T Instance + { + get + { + if (null == sInstance) + { + sInstance = new T(); + TLogger.LogAssert(sInstance != null); + sInstance.Awake(); + RegSingleton(sInstance); + } + + return sInstance; + } + } + + private static void RegSingleton(BaseBehaviourSingleton inst) + { + BehaviourSingleSystem.Instance.RegSingleton(inst); + } + } + + public class BaseBehaviourSingleton + { + public bool IsStart = false; + + 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 class BehaviourSingleSystem : BaseLogicSys + { + List m_listInst = new List(); + List m_listStart = new List(); + List m_listUpdate = new List(); + List m_listLateUpdate = new List(); + + public void RegSingleton(BaseBehaviourSingleton inst) + { + TLogger.LogAssert(!m_listInst.Contains(inst)); + m_listInst.Add(inst); + m_listStart.Add(inst); + } + + public override void OnUpdate() + { + var listStart = m_listStart; + + var listToUpdate = m_listUpdate; + var listToLateUpdate = m_listLateUpdate; + + if (listStart.Count > 0) + { + for (int i = 0; i < listStart.Count; i++) + { + var inst = listStart[i]; + TLogger.LogAssert(!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 = m_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 < m_listInst.Count; i++) + { + var inst = m_listInst[i]; + inst.Destroy(); + } + } + + public override void OnPause() + { + for (int i = 0; i < m_listInst.Count; i++) + { + var inst = m_listInst[i]; + inst.OnPause(); + } + } + + public override void OnResume() + { + for (int i = 0; i < m_listInst.Count; i++) + { + var inst = m_listInst[i]; + inst.OnResume(); + } + } + } +} diff --git a/Assets/TEngine/Runtime/Core/BehaviourSingleton.cs.meta b/Assets/TEngine/Runtime/Core/BehaviourSingleton.cs.meta new file mode 100644 index 00000000..5e66a326 --- /dev/null +++ b/Assets/TEngine/Runtime/Core/BehaviourSingleton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 97c54c57df9a999408fcc292927654b4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Core/TLogger.cs b/Assets/TEngine/Runtime/Core/TLogger.cs index 590f3048..e3aa06ca 100644 --- a/Assets/TEngine/Runtime/Core/TLogger.cs +++ b/Assets/TEngine/Runtime/Core/TLogger.cs @@ -86,10 +86,16 @@ namespace TEngine } [Conditional("UNITY_EDITOR"), Conditional("_DEVELOPMENT_BUILD_"), Conditional("ENABLE_LOG_ASSERT")] - public static void LogAssert(bool condition, string logStr) + public static void LogAssert(bool condition, string logStr = "") { if (!condition) + { + if (string.IsNullOrEmpty(logStr)) + { + logStr = string.Format("{0}", "Assert Failed"); + } Instance.Log(LogLevel.ASSERT, logStr); + } } [Conditional("UNITY_EDITOR"), Conditional("_DEVELOPMENT_BUILD_"), Conditional("ENABLE_LOG_ASSERT")] diff --git a/Assets/TEngine/Runtime/Core/TProfiler.cs b/Assets/TEngine/Runtime/Core/TProfiler.cs new file mode 100644 index 00000000..70723f2e --- /dev/null +++ b/Assets/TEngine/Runtime/Core/TProfiler.cs @@ -0,0 +1,67 @@ +using System.Diagnostics; +using UnityEngine.Profiling; + +namespace TEngine +{ + public class TProfiler + { + private static int m_profileLevel = -1; + private static int m_currLevel = 0; + private static int m_sampleLevel = 0; + + public static void SetProfileLevel(int level) + { + m_profileLevel = level; + } + + [Conditional("UNITY_EDITOR")] + public static void BeginFirstSample(string name) + { + m_currLevel++; + if (m_profileLevel >= 0 && m_currLevel > m_profileLevel) + { + return; + } + + m_sampleLevel++; + Profiler.BeginSample(name); + } + + [Conditional("UNITY_EDITOR")] + public static void EndFirstSample() + { + if (m_currLevel <= m_sampleLevel) + { + Profiler.EndSample(); + m_sampleLevel--; + } + + m_currLevel--; + } + + [Conditional("UNITY_EDITOR")] + public static void BeginSample(string name) + { + m_currLevel++; + if (m_profileLevel >= 0 && m_currLevel > m_profileLevel) + { + return; + } + + m_sampleLevel++; + Profiler.BeginSample(name); + } + + [Conditional("UNITY_EDITOR")] + public static void EndSample() + { + if (m_currLevel <= m_sampleLevel) + { + Profiler.EndSample(); + m_sampleLevel--; + } + + m_currLevel--; + } + } +} diff --git a/Assets/TEngine/Runtime/Core/TProfiler.cs.meta b/Assets/TEngine/Runtime/Core/TProfiler.cs.meta new file mode 100644 index 00000000..27d68a7e --- /dev/null +++ b/Assets/TEngine/Runtime/Core/TProfiler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0c56157481b6d0047bcc5b754abb81b1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/UI/Demo/TEngineTest.cs b/Assets/TEngine/Runtime/UI/Demo/TEngineTest.cs index a030c00e..9841274c 100644 --- a/Assets/TEngine/Runtime/UI/Demo/TEngineTest.cs +++ b/Assets/TEngine/Runtime/UI/Demo/TEngineTest.cs @@ -13,6 +13,7 @@ public class TEngineTest : TEngine.TEngine { base.RegisterAllSystem(); //注册系统,例如UI系统,网络系统,战斗系统等等 + AddLogicSys(BehaviourSingleSystem.Instance); AddLogicSys(UISys.Instance); }