using System; using System.Collections.Generic; using AlicizaX.Runtime; using UnityEngine; namespace AlicizaX.Timer.Runtime { /// /// 计时器组件。 /// [DisallowMultipleComponent] [AddComponentMenu("Game Framework/Timer")] [UnityEngine.Scripting.Preserve] public class TimerComponent : GameFrameworkComponent { ITimerManager _timerManager; protected override void Awake() { ImplementationComponentType = Utility.Assembly.GetType(componentType); InterfaceComponentType = typeof(ITimerManager); base.Awake(); _timerManager = SysModuleCenter.GetModule(); if (_timerManager == null) { Log.Fatal("Timer manager is invalid."); return; } } /// /// 添加一个定时调用的任务 /// /// 间隔时间(以秒为单位) /// 重复次数(0 表示无限重复) /// 要执行的回调函数 /// 回调函数的参数(可选) public void Add(float interval, int repeat, Action callback, object callbackParam = null) { _timerManager.Add(interval, repeat, callback, callbackParam); } /// /// 添加一个只执行一次的任务 /// /// 间隔时间(以秒为单位) /// 要执行的回调函数 /// 回调函数的参数(可选) public void AddOnce(float interval, Action callback, object callbackParam = null) { _timerManager.AddOnce(interval, callback, callbackParam); } /// /// 添加一个每帧更新执行的任务 /// /// 要执行的回调函数 public void AddUpdate(Action callback) { _timerManager.AddUpdate(callback); } /// /// 添加一个每帧更新执行的任务 /// /// 要执行的回调函数 /// 回调函数的参数 public void AddUpdate(Action callback, object callbackParam) { _timerManager.AddUpdate(callback, callbackParam); } /// /// 检查指定的任务是否存在 /// /// 要检查的回调函数 /// 存在返回 true,不存在返回 false public bool Exists(Action callback) { return _timerManager.Exists(callback); } /// /// 移除指定的任务 /// /// 要移除的回调函数 public void Remove(Action callback) { _timerManager.Remove(callback); } public List GetTimerInfos() { return _timerManager.GetTimerInfos(); } } }