using System.Collections; using System.Collections.Generic; using UnityEngine; namespace YKMoon { internal interface IUpdater { void Update(float deltaTime, float unscaledDeltaTime); void Clear(); } internal enum UpdaterType { Normal, Late, Seconds, Custom } public partial class UpdaterManager : USingletonMono, IUSingletonEventHandler { /// /// 普通Update事件管理 /// public static NormalUpdater normalUpdater { get => Instance.updaterDict[UpdaterType.Normal] as NormalUpdater; } /// /// LateUpdate事件管理 /// public static NormalUpdater lateUpdater { get => Instance.updaterDict[UpdaterType.Late] as NormalUpdater; } /// /// 客户端全局不受时间缩放的秒计时器 /// 可用于需要倒计时的地方 /// public static SecondsUpdater secondsUpdater { get => Instance.updaterDict[UpdaterType.Seconds] as SecondsUpdater; } /// /// 自定义时间间隔Updater /// public static CustomUpdater customUpdater { get => Instance.updaterDict[UpdaterType.Custom] as CustomUpdater; } private Dictionary updaterDict = new Dictionary(); private bool isInitialized = false; public void OnCreateSingleton() { Init(); } private void Init() { if (isInitialized) { return; } updaterDict.Add(UpdaterType.Normal, new NormalUpdater()); updaterDict.Add(UpdaterType.Late, new NormalUpdater()); updaterDict.Add(UpdaterType.Seconds, new SecondsUpdater()); updaterDict.Add(UpdaterType.Custom, new CustomUpdater()); isInitialized = true; } /// /// 清空所有事件 /// public static void Clear() { foreach(var updater in Instance.updaterDict.Values) { updater.Clear(); } } private void Update() { normalUpdater?.Update(Time.deltaTime, Time.unscaledDeltaTime); customUpdater?.Update(Time.deltaTime, Time.unscaledDeltaTime); secondsUpdater?.Update(Time.deltaTime, Time.unscaledDeltaTime); } private void LateUpdate() { lateUpdater?.Update(Time.deltaTime, Time.unscaledDeltaTime); } } }