简易的计时器 满足一些延时调用的需求
这里用到了单例继承 SingletonBase.cs 在我另一篇文章里
地址点击即可: SingletonBase.cs 源码
using System;
using System.Collections.Generic;
using UnityEngine;
public class TimerData
{
public float delay { get; private set; } //延迟时间
public Action callback { get; private set; } //回调方法
public bool repeat { get; private set; } //是否重复调用
public TimerData(float delay, Action callback, bool repeat = false)
{
this.delay = delay;
this.callback = callback;
this.repeat = repeat;
}
}
//定时器回调脚本
public class TimerEvent
{
//定时器字典 存储回调内容
private List<TimerData> _eventList = new List<TimerData>();
private List<float> _timerList = new List<float>();
/// <summary>
/// 初始化定时器
/// </summary>
/// <param name="delay">倒计时时间</param>
/// <param name="callback">回调方法</param>
/// <param name="repeat">是否重复(循环)调用</param>
public TimerEvent(float delay, Action callback, bool repeat = false)
{
this.AddListener(delay, callback, repeat);
}
/// <summary>
/// 空的构造函数时需要自己手动 Add
/// </summary>
public TimerEvent() {}
/// <summary>
/// 添加定时器
/// </summary>
/// <param name="delay">倒计时时间</param>
/// <param name="callback">回调方法</param>
/// <param name="repeat">是否重复(循环)调用</param>
public void AddListener(float delay, Action callback, bool repeat = false)
{
TimerData timeO = new TimerData(delay, callback, repeat);
if (!this._eventList.Equals(timeO)) {
this._eventList.Add(timeO);
_timerList.Add(0f);
}
}
/// <summary>
/// 移除定时器
/// </summary>
/// <param name="timerOwner"></param>
private void RemoveListener(TimerData timerOwner)
{
if (!this._eventList.Equals(timerOwner))
{
this._eventList.Remove(timerOwner);
}
}
/// <summary>
/// 开始定时器
/// </summary>
public void Start()
{
if (_eventList.Count < 1)
{
Debug.Log("暂无任何定时器!");
}
else {
TimerCall.Instance.Add(this);
}
}
/// <summary>
/// 暂停定时器
/// </summary>
public void Stop() {
if (_eventList.Count < 1)
{
Debug.Log("暂无任何定时器!");
}
else {
this.Clear();
}
}
/// <summary>
/// 清理定时器
/// </summary>
private void Clear()
{
TimerCall.Instance.Remove(this);
this._eventList.Clear();
this._timerList.Clear();
}
public void Update(float deltaTime ) {
for (int i = 0; i < _eventList.Count; i++)
{
TimerData timerOwner = _eventList[i];
_timerList[i] += deltaTime;
if (_timerList[i] >= timerOwner.delay) {
timerOwner.callback?.Invoke();
if (!timerOwner.repeat)
{
this.RemoveListener(timerOwner);
this._timerList.RemoveAt(i);
}
else
{
if(_timerList.Count>i)
_timerList[i] = 0;
}
}
}
}
}
public class TimerCall : SingletonBase<TimerCall>
{
private List<TimerEvent> _timerEvent = new List<TimerEvent>();
public void Add(TimerEvent timerEvent)
{
if (!this._timerEvent.Equals(timerEvent))
this._timerEvent.Add(timerEvent);
}
public void Remove(TimerEvent timerEvent)
{
for (int i = 0; i < this._timerEvent.Count; i++)
{
if (this._timerEvent[i] == timerEvent) {
this._timerEvent.RemoveAt(i);
}
}
}
public void RemoveAll()
{
this._timerEvent.Clear();
}
void Update()
{
for (int i = 0; i < _timerEvent.Count; i++)
{
this._timerEvent[i]?.Update(Time.deltaTime);
}
}
}
下边是调用方法
void Start()
{
//调用 1 初始化一个定时器
TimerEvent timerEvent = new TimerEvent(1f, () => {
Debug.Log(" 每1秒调用一次 ");
}, true);
//再次增加一个定时器
timerEvent.AddListener(3f, () =>
{
Debug.Log("3秒的时候调仅用一次 ");
});
//开始执行
timerEvent.Start();
/***************************************************************/
//调用2
TimerEvent timerEvent2 = new TimerEvent();
timerEvent2.AddListener(4f, () => {
Debug.Log("4秒的时候调仅用一次 ");
});
//开始执行
timerEvent2.Start();
}