123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- namespace XRTool.Util
- {
- public delegate void TimerHandler();
- public delegate void TimerArgsHandler(object[] args);
- public class Timer
- {
- public TimerHandler Handler; //无参的委托
- public TimerArgsHandler ArgsHandler; //带参数的委托
- public float Frequency; //时间间隔
- public int Repeats; //重复次数
- public object[] Args;
- public float LastTickTime;
- public event Action OnComplete; //计时器完成一次工作
- public event Action OnDestroy; //计时器被销毁
- public event Action OnFinsh; //计时器结束工作了
- public Timer() { }
- /// <summary>
- /// 创建一个时间事件对象
- /// </summary>
- /// <param name="Handler">回调函数</param>
- /// <param name="ArgsHandler">带参数的回调函数</param>
- /// <param name="frequency">时间内执行</param>
- /// <param name="repeats">重复次数</param>
- /// <param name="Args">参数 可以任意的传不定数量,类型的参数</param>
- public Timer(TimerHandler Handler, TimerArgsHandler ArgsHandler, float frequency, int repeats, object[] Args)
- {
- this.Handler = Handler;
- this.ArgsHandler = ArgsHandler;
- this.Frequency = frequency;
- this.Repeats = repeats == 0 ? 1 : repeats;
- this.Args = Args;
- this.LastTickTime = Time.time;
- }
- public void Notify()
- {
- Handler?.Invoke();
- ArgsHandler?.Invoke(Args);
- OnComplete?.Invoke();
- }
- /// <summary>
- /// 清理计时器,初始化参数 同时清理事件
- /// </summary>
- public void CleanUp()
- {
- Handler = null;
- ArgsHandler = null;
- Repeats = 1;
- Frequency = 0;
- OnDestroy?.Invoke();
- OnFinsh?.Invoke();
- OnDestroy = null;
- OnComplete = null;
- }
- /// <summary>
- /// 立即执行回调
- /// isBreak = false的时候需要根据是否继续执行,传time
- /// isBreak = true,可不传time
- /// </summary>
- /// <param name="time">当前的时间,Time.time</param>
- /// <param name="isBreak">执行完之后是否销毁计时器</param>
- public void AwakeNow(float time = 0, bool isBreak = true)
- {
- try
- {
- ///唤醒可能会有异常
- Notify();
- }
- catch (Exception ex)
- {
- UnityLog.LogException(ex);
- UnityLog.LogError(ex.ToString());
- }
- finally
- {
- if (isBreak)
- {
- OnFinsh?.Invoke();
- TimerMgr.Instance.DestroyTimer(this);
- }
- else
- {
- if ((--Repeats) == 0)
- {
- OnFinsh?.Invoke();
- TimerMgr.Instance.DestroyTimer(this);
- }
- else
- {
- LastTickTime = time;
- }
- }
- }
- }
- /// <summary>
- /// 根据当前时间判断是否要触发触发器
- /// </summary>
- /// <param name="time"></param>
- public void Update(float time)
- {
- if (Frequency + LastTickTime <= time)
- {
- AwakeNow(time, false);
- }
- }
- }
- /// <summary>
- /// 计时器
- /// 添加一个计时事件
- /// 删除一个计时事件
- /// 更新计时事件
- /// </summary>
- public class TimerMgr : UnitySingleton<TimerMgr>
- {
- private List<Timer> _Timers;//时间管理器
- protected override void Awake()
- {
- DontDestroyOnLoad(gameObject);
- if (_Timers == null)
- {
- _Timers = new List<Timer>();
- }
- base.Awake();
- }
- /// <summary>
- /// 创建一个简单的计时器
- /// </summary>
- /// <param name="callBack">回调函数</param>
- /// <param name="time">计时器时间</param>
- /// <param name="repeats">回调次数 小于0代表循环 大于0代表repeats次</param>
- public Timer CreateTimer(TimerHandler callBack, float time, int repeats = 1)
- {
- return Create(callBack, null, time, repeats);
- }
- public Timer CreateTimer(TimerArgsHandler callBack, float time, int repeats, params object[] args)
- {
- return Create(null, callBack, time, repeats, args);
- }
- private Timer Create(TimerHandler callBack, TimerArgsHandler callBackArgs, float time, int repeats, params object[] args)
- {
- Timer timer = new Timer(callBack, callBackArgs, time, repeats, args);
- _Timers.Add(timer);
- return timer;
- }
- public Timer DestroyTimer(Timer timer)
- {
- if (timer != null)
- {
- _Timers.Remove(timer);
- timer.CleanUp();
- timer = null;
- }
- return timer;
- }
- /// <summary>
- /// 禁止调用此函数
- /// </summary>
- public void ClearAll()
- {
- if (_Timers != null)
- {
- for (int i = 0; i < _Timers.Count; i++)
- {
- _Timers[i].CleanUp();
- }
- _Timers.Clear();
- }
- }
- /// <summary>
- /// 固定更新检查更新的频率
- /// </summary>
- private void Update()
- {
- if (_Timers.Count != 0)
- {
- for (int i = _Timers.Count - 1; i >= 0 && i < _Timers.Count; i--)
- {
- _Timers[i].Update(Time.time);
- }
- }
- }
- //private void OnEnable()
- //{
- // ScenceMain.MainUpdateEvent += MainUpdate;
- //}
- //private void OnDisable()
- //{
- // ScenceMain.MainUpdateEvent -= MainUpdate;
- //}
- }
- }
|