using System.Collections; using System.Collections.Generic; using UnityEngine; namespace YKMoon { public partial class CustomUpdater { internal class UpdateEventData { private float timeInterval; private float timeCount; private bool unscaled = false; private System.Action updateEvent; internal UpdateEventData(System.Action updateEvent, float timeInterval = 1f, bool unscaled = false) { this.updateEvent = updateEvent; this.timeInterval = timeInterval; this.unscaled = unscaled; this.timeCount = 0f; } internal void Update(float deltaTime, float unscaledDeltaTime) { float deltaT = unscaled ? unscaledDeltaTime : deltaTime; timeCount += deltaT; if(timeCount >= timeInterval) { timeCount -= timeInterval; updateEvent?.Invoke(timeInterval); } } } } public partial class CustomUpdater : IUpdater { private Dictionary updaters = new Dictionary(); /// /// Register update event to this updater. /// /// (float deltaTime) /// time internal. /// is unscaled updater? public void Reg(System.Action updateEvent, float timeInterval = 1f, bool unscaled = false) { updaters.Add(updateEvent.GetHashCode(), new UpdateEventData(updateEvent, timeInterval, unscaled)); } /// /// Unregister update event in this updater. /// public void UnReg(System.Action updateEvent) { updaters.Remove(updateEvent.GetHashCode()); } public void Update(float deltaTime, float unscaledDeltaTime) { var dict = DictPool.Get(); //Cache updaters in temp dict.Ensure iterative safety. foreach(var pair in updaters) { dict.Add(pair.Key, pair.Value); } foreach(var data in dict.Values) { data.Update(deltaTime, unscaledDeltaTime); } DictPool.Release(dict); } public void Clear() { updaters.Clear(); } } }