频繁开启关闭计时协程,让我利用上篇的内容想写一个计时器,我打算写成类似dotween那样的链式编程。于是就有了下面的Timer类。一个Timer对象就是一个计时器,他会开启一个协程,完成任务后会重新回到池子等待下一次分配。
使用的时候记得标记上出处,禁止转载。上篇内容--https://blog.csdn.net/weixin_42977419/article/details/108446243
namespace ToolsByLm.Timer
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Timer
{
/// <summary>
/// 要执行的回调
/// </summary>
private TimerCallBack callBack;
/// <summary>
/// 创建时的回调
/// </summary>
private TimerCallBack onCreated;
/// <summary>
/// 产生的时候
/// </summary>
private TimerCallBack onAwake;
/// <summary>
/// Timer开始计时的时候
/// </summary>
private TimerCallBack onStart;
/// <summary>
/// Timer销毁的时候
/// </summary>
private TimerCallBack onDestroy;
private IEnumerator enumerator;
private bool disposed = false;
private int id;
/// <summary>
/// 是否已经启用
/// </summary>
public bool enabled { get; set; } = true;
public Timer myself
{
get
{
if (disposed)
{
return null;
}
return this;
}
}
private void SetTimer(float time, TimerCallBack created = null)
{
disposed = false;
onCreated += created;
enumerator = CallBack(time);
IEnumeratorCenter.StartCoroutineTask(Created());
}
private void SetTimer(ushort frame, TimerCallBack created = null)
{
onCreated += created;
enumerator = CallBack(frame);
IEnumeratorCenter.StartCoroutineTask(Created());
}
IEnumerator Created()
{
onCreated?.Invoke();
yield return null;
if (enabled)
{
Enabled();
}
}
/// <summary>
/// 启用
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public Timer Enabled()
{
enabled = true;
onAwake?.Invoke();
IEnumeratorCenter.StartCoroutineTask(enumerator);
return myself;
}
IEnumerator CallBack(float time)
{
onStart?.Invoke();
yield return new WaitForSeconds(time);
callBack?.Invoke();
Kill();
}
IEnumerator CallBack(ushort frame)
{
onStart?.Invoke();
while (frame > 0)
{
frame--;
yield return frame;
}
callBack?.Invoke();
yield return null;
Kill();
}
/// <summary>
/// 注册唤醒时的回调
/// </summary>
/// <returns></returns>
public Timer OnAwake(TimerCallBack awake)
{
onAwake += awake;
return myself;
}
public Timer OnStart(TimerCallBack start)
{
onStart += start;
return myself;
}
public Timer OnDestroy(TimerCallBack destroy)
{
onDestroy += destroy;
return myself;
}
public Timer SetCallBack(TimerCallBack callBack)
{
this.callBack += callBack;
return myself;
}
public Timer Kill()
{
IEnumeratorCenter.StopCoroutineTask(enumerator);
onDestroy?.Invoke();
Reset();
disposed = true;
return myself;
}
private void Reset()
{
enumerator = null;
onCreated = onAwake = onDestroy = onStart = callBack = null;
}
private static List<Timer> timerspool = new List<Timer>();
private static Timer GetTimer()
{
Timer timer = null;
for (int i = 0; i < timerspool.Count; i++)
{
timer = timerspool[i];
if (timer.disposed)
{
return timer;
}
}
timer = new Timer();
timer.id = timerspool.Count;
timerspool.Add(timer);
return timer.myself;
}
/// <summary>
/// 创建一个计时器
/// <example><code>Timer.Creator(1,
/// ()=>
/// {
/// print("你要执行的回调");
/// })</code></example>
/// </summary>
/// <param name="time">计时的时间</param>
/// <param name="created">需要执行的回调</param>
/// <returns></returns>
public static Timer Creator(float time, TimerCallBack created = null)
{
Timer timer = GetTimer();
timer.SetTimer(time, created);
return timer.myself;
}
public static Timer Creator(ushort frame, TimerCallBack created = null, bool useframe = false)
{
Timer timer = GetTimer();
if (useframe)
{
timer.SetTimer(frame, created);
}
else
{
timer.SetTimer((float)frame, created);
}
return timer.myself;
}
public static Timer Creator(ushort frame, bool useframe)
{
Timer timer = GetTimer();
if (useframe)
{
timer.SetTimer(frame);
}
else
{
timer.SetTimer((float)frame);
}
return timer.myself;
}
}
public delegate void TimerCallBack();
}
使用的时候记得标记上出处,禁止转载。
//代码示例
Timer.Creator(1, () =>
{
Debug.LogError("杺颜原创!");
});