mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
统一Update,实现Priority优先级轮询
统一Update,实现Priority优先级轮询
This commit is contained in:
@@ -403,13 +403,13 @@ namespace TEngine.Runtime
|
||||
AudioClipPool.Clear();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
||||
{
|
||||
for (int i = 0; i < _audioAgents.Length; ++i)
|
||||
{
|
||||
if (_audioAgents[i] != null)
|
||||
{
|
||||
_audioAgents[i].Update(Time.deltaTime);
|
||||
_audioAgents[i].Update(realElapseSeconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -9,11 +9,24 @@ namespace TEngine.Runtime
|
||||
/// </summary>
|
||||
public interface ISingleton
|
||||
{
|
||||
/// <summary>
|
||||
/// 激活
|
||||
/// </summary>
|
||||
void Active();
|
||||
|
||||
/// <summary>
|
||||
/// 释放
|
||||
/// </summary>
|
||||
void Release();
|
||||
}
|
||||
|
||||
public interface IUnitySingleton
|
||||
{
|
||||
int GetPriority();
|
||||
|
||||
void OnUpdate(float elapseSeconds, float realElapseSeconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 单例管理器(统一化持久和释放)
|
||||
/// </summary>
|
||||
@@ -21,6 +34,7 @@ namespace TEngine.Runtime
|
||||
{
|
||||
private static List<ISingleton> _iSingletonList;
|
||||
private static Dictionary<string, GameObject> _gameObjects;
|
||||
private static List<IUnitySingleton> _unitySingletons;
|
||||
private static GameObject _root;
|
||||
public static GameObject Root
|
||||
{
|
||||
@@ -119,7 +133,7 @@ namespace TEngine.Runtime
|
||||
return go;
|
||||
}
|
||||
|
||||
internal static bool CointansKey(string name)
|
||||
internal static bool ContainsKey(string name)
|
||||
{
|
||||
if (_gameObjects != null)
|
||||
{
|
||||
|
@@ -6,7 +6,7 @@ namespace TEngine.Runtime
|
||||
/// 具备Unity完整生命周期的单例
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class UnitySingleton<T> : MonoBehaviour where T : MonoBehaviour
|
||||
public class UnitySingleton<T> : MonoBehaviour,IUnitySingleton where T : MonoBehaviour
|
||||
{
|
||||
|
||||
private static T _instance;
|
||||
@@ -86,6 +86,7 @@ namespace TEngine.Runtime
|
||||
{
|
||||
if (CheckInstance())
|
||||
{
|
||||
UpdateInstance.Instance.Retain(this);
|
||||
OnLoad();
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
@@ -98,8 +99,42 @@ namespace TEngine.Runtime
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
/// <summary>
|
||||
/// 获取游戏框架模块优先级。实现Interface
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <remarks>优先级较高的模块会优先轮询,并且关闭操作会后进行。</remarks>
|
||||
public int GetPriority()
|
||||
{
|
||||
return Priority;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取游戏框架模块优先级。
|
||||
/// </summary>
|
||||
/// <remarks>优先级较高的模块会优先轮询,并且关闭操作会后进行。</remarks>
|
||||
public virtual int Priority
|
||||
{
|
||||
get
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// OnUpdate通过TEngine统一驱动,舍弃Unity的Update
|
||||
/// </summary>
|
||||
/// <param name="elapseSeconds"></param>
|
||||
/// <param name="realElapseSeconds"></param>
|
||||
public virtual void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public virtual void OnDestroy()
|
||||
{
|
||||
UpdateInstance.Instance.Release(this);
|
||||
Release();
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,73 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TEngine.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 实现UnitySingleton的OnUpdate
|
||||
/// </summary>
|
||||
internal class UpdateInstance : BehaviourSingleton<UpdateInstance>
|
||||
{
|
||||
public List<IUnitySingleton> UnitySingletons;
|
||||
|
||||
public UpdateInstance()
|
||||
{
|
||||
UnitySingletons = new List<IUnitySingleton>();
|
||||
}
|
||||
|
||||
public void Retain(IUnitySingleton unitySingleton)
|
||||
{
|
||||
if (UnitySingletons.Contains(unitySingleton))
|
||||
{
|
||||
Log.Fatal($"Repeat Retain UnitySingleton{unitySingleton}");
|
||||
}
|
||||
else
|
||||
{
|
||||
UnitySingletons.Add(unitySingleton);
|
||||
|
||||
UnitySingletons.Sort((x, y) => -x.GetPriority().CompareTo(y.GetPriority()));
|
||||
}
|
||||
|
||||
for (int i = 0; i < UnitySingletons.Count; i++)
|
||||
{
|
||||
Log.Fatal(UnitySingletons[i].ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void Release(IUnitySingleton unitySingleton)
|
||||
{
|
||||
if (UnitySingletons.Contains(unitySingleton))
|
||||
{
|
||||
UnitySingletons.Remove(unitySingleton);
|
||||
}
|
||||
}
|
||||
|
||||
public void ReleaseAll()
|
||||
{
|
||||
var count = UnitySingletons.Count;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Release(UnitySingletons[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
var count = UnitySingletons.Count;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
UnitySingletons[i].OnUpdate(UnityEngine.Time.deltaTime, UnityEngine.Time.unscaledDeltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
public override void LateUpdate()
|
||||
{
|
||||
base.LateUpdate();
|
||||
}
|
||||
|
||||
public override void Destroy()
|
||||
{
|
||||
ReleaseAll();
|
||||
base.Destroy();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0018c33e90584ee7a269d84609c606b0
|
||||
timeCreated: 1662259264
|
@@ -143,7 +143,7 @@ namespace TEngine.Runtime
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
public override void OnDestroy()
|
||||
{
|
||||
for (int i = 0; i < m_LogicMgrList.Count; i++)
|
||||
{
|
||||
@@ -151,7 +151,6 @@ namespace TEngine.Runtime
|
||||
logicSys.OnDestroy();
|
||||
}
|
||||
base.OnDestroy();
|
||||
SingletonMgr.Release();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
@@ -25,7 +25,7 @@ namespace TEngine.Runtime
|
||||
/// 获取游戏框架模块优先级。
|
||||
/// </summary>
|
||||
/// <remarks>优先级较高的模块会优先轮询,并且关闭操作会后进行。</remarks>
|
||||
internal int Priority
|
||||
public override int Priority
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -48,7 +48,7 @@ namespace TEngine.Runtime
|
||||
/// </summary>
|
||||
/// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
|
||||
/// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
|
||||
public void Update()
|
||||
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
||||
{
|
||||
m_TempFsms.Clear();
|
||||
if (m_Fsms.Count <= 0)
|
||||
@@ -68,14 +68,14 @@ namespace TEngine.Runtime
|
||||
continue;
|
||||
}
|
||||
|
||||
fsm.Update(Time.deltaTime, Time.unscaledDeltaTime);
|
||||
fsm.Update(elapseSeconds, realElapseSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭并清理有限状态机管理器。
|
||||
/// </summary>
|
||||
protected override void OnDestroy()
|
||||
public override void OnDestroy()
|
||||
{
|
||||
foreach (KeyValuePair<TypeNamePair, FsmBase> fsm in m_Fsms)
|
||||
{
|
||||
|
@@ -19,6 +19,13 @@ namespace TEngine.Runtime
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取游戏框架模块优先级。
|
||||
/// </summary>
|
||||
/// <remarks>优先级较高的模块会优先轮询,并且关闭操作会后进行。</remarks>
|
||||
public override int Priority => 10;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取网络频道数量。
|
||||
/// </summary>
|
||||
@@ -52,15 +59,15 @@ namespace TEngine.Runtime
|
||||
m_NetworkManager.NetworkCustomError += OnNetworkCustomError;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
||||
{
|
||||
if (m_NetworkManager != null)
|
||||
{
|
||||
NetworkManager.Update(Time.deltaTime, Time.unscaledDeltaTime);;
|
||||
NetworkManager.Update(elapseSeconds, realElapseSeconds);;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
public override void OnDestroy()
|
||||
{
|
||||
base.OnDestroy();
|
||||
if (NetworkManager != null)
|
||||
|
Reference in New Issue
Block a user