mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
增加了BehaviorSingleton
增加了BehaviorSingleton
This commit is contained in:
165
Assets/TEngine/Runtime/Core/BehaviourSingleton.cs
Normal file
165
Assets/TEngine/Runtime/Core/BehaviourSingleton.cs
Normal file
@@ -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<T> : 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<BehaviourSingleSystem>
|
||||
{
|
||||
List<BaseBehaviourSingleton> m_listInst = new List<BaseBehaviourSingleton>();
|
||||
List<BaseBehaviourSingleton> m_listStart = new List<BaseBehaviourSingleton>();
|
||||
List<BaseBehaviourSingleton> m_listUpdate = new List<BaseBehaviourSingleton>();
|
||||
List<BaseBehaviourSingleton> m_listLateUpdate = new List<BaseBehaviourSingleton>();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Core/BehaviourSingleton.cs.meta
Normal file
11
Assets/TEngine/Runtime/Core/BehaviourSingleton.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 97c54c57df9a999408fcc292927654b4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -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")]
|
||||
|
67
Assets/TEngine/Runtime/Core/TProfiler.cs
Normal file
67
Assets/TEngine/Runtime/Core/TProfiler.cs
Normal file
@@ -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--;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Core/TProfiler.cs.meta
Normal file
11
Assets/TEngine/Runtime/Core/TProfiler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c56157481b6d0047bcc5b754abb81b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -13,6 +13,7 @@ public class TEngineTest : TEngine.TEngine
|
||||
{
|
||||
base.RegisterAllSystem();
|
||||
//注册系统,例如UI系统,网络系统,战斗系统等等
|
||||
AddLogicSys(BehaviourSingleSystem.Instance);
|
||||
AddLogicSys(UISys.Instance);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user