mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
Update
Update
This commit is contained in:
@@ -20,13 +20,6 @@ namespace TEngine.Runtime
|
|||||||
void Release();
|
void Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IUnitySingleton
|
|
||||||
{
|
|
||||||
int GetPriority();
|
|
||||||
|
|
||||||
void OnUpdate(float elapseSeconds, float realElapseSeconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 单例管理器(统一化持久和释放)
|
/// 单例管理器(统一化持久和释放)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -34,7 +27,6 @@ namespace TEngine.Runtime
|
|||||||
{
|
{
|
||||||
private static List<ISingleton> _iSingletonList;
|
private static List<ISingleton> _iSingletonList;
|
||||||
private static Dictionary<string, GameObject> _gameObjects;
|
private static Dictionary<string, GameObject> _gameObjects;
|
||||||
private static List<IUnitySingleton> _unitySingletons;
|
|
||||||
private static GameObject _root;
|
private static GameObject _root;
|
||||||
public static GameObject Root
|
public static GameObject Root
|
||||||
{
|
{
|
||||||
|
@@ -6,7 +6,7 @@ namespace TEngine.Runtime
|
|||||||
/// 具备Unity完整生命周期的单例
|
/// 具备Unity完整生命周期的单例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class UnitySingleton<T> : MonoBehaviour,IUnitySingleton where T : MonoBehaviour
|
public class UnitySingleton<T> : MonoBehaviour,IUpdateSystem where T : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
|
||||||
private static T _instance;
|
private static T _instance;
|
||||||
|
@@ -2,55 +2,62 @@
|
|||||||
|
|
||||||
namespace TEngine.Runtime
|
namespace TEngine.Runtime
|
||||||
{
|
{
|
||||||
|
public interface IUpdateSystem
|
||||||
|
{
|
||||||
|
int GetPriority();
|
||||||
|
|
||||||
|
void OnUpdate(float elapseSeconds, float realElapseSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 实现UnitySingleton的OnUpdate
|
/// 实现UnitySingleton的OnUpdate
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class UpdateInstance : BehaviourSingleton<UpdateInstance>
|
internal class UpdateInstance : BehaviourSingleton<UpdateInstance>
|
||||||
{
|
{
|
||||||
public List<IUnitySingleton> UnitySingletons;
|
public List<IUpdateSystem> UpdateSystems;
|
||||||
|
|
||||||
public UpdateInstance()
|
public UpdateInstance()
|
||||||
{
|
{
|
||||||
UnitySingletons = new List<IUnitySingleton>();
|
UpdateSystems = new List<IUpdateSystem>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Retain(IUnitySingleton unitySingleton)
|
public void Retain(IUpdateSystem updateSystem)
|
||||||
{
|
{
|
||||||
if (UnitySingletons.Contains(unitySingleton))
|
if (UpdateSystems.Contains(updateSystem))
|
||||||
{
|
{
|
||||||
Log.Fatal($"Repeat Retain UnitySingleton{unitySingleton}");
|
Log.Fatal($"Repeat Retain UnitySingleton{updateSystem}");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
UnitySingletons.Add(unitySingleton);
|
UpdateSystems.Add(updateSystem);
|
||||||
|
|
||||||
UnitySingletons.Sort((x, y) => -x.GetPriority().CompareTo(y.GetPriority()));
|
UpdateSystems.Sort((x, y) => -x.GetPriority().CompareTo(y.GetPriority()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Release(IUnitySingleton unitySingleton)
|
public void Release(IUpdateSystem updateSystem)
|
||||||
{
|
{
|
||||||
if (UnitySingletons.Contains(unitySingleton))
|
if (UpdateSystems.Contains(updateSystem))
|
||||||
{
|
{
|
||||||
UnitySingletons.Remove(unitySingleton);
|
UpdateSystems.Remove(updateSystem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReleaseAll()
|
public void ReleaseAll()
|
||||||
{
|
{
|
||||||
var count = UnitySingletons.Count;
|
var count = UpdateSystems.Count;
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = count-1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
Release(UnitySingletons[i]);
|
Release(UpdateSystems[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Update()
|
public override void Update()
|
||||||
{
|
{
|
||||||
var count = UnitySingletons.Count;
|
var count = UpdateSystems.Count;
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
UnitySingletons[i].OnUpdate(UnityEngine.Time.deltaTime, UnityEngine.Time.unscaledDeltaTime);
|
UpdateSystems[i].OnUpdate(UnityEngine.Time.deltaTime, UnityEngine.Time.unscaledDeltaTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user