一,自己封装的
基类BaseCD ,在Awake或者Start中new, 在Update中调用 StartCD 。 目前实现了2个子类。
1⃣️,基类(BaseCD)
using System; namespace lib { /// <summary> /// 倒计时基类 /// </summary> public abstract class BaseCD { protected int delay; protected float duration; protected Action<TYCD, int> callback; protected bool everyCallback;//每次执行callback protected int repeatCount;//总共执行的次数 protected int doCount; protected bool playing; public BaseCD(Action<TYCD, int> callback, bool everyCallback, int repeatCount,int delay =0 , float duration = 1) { this.Reset(callback,everyCallback,repeatCount,delay, duration); } public virtual void Reset(Action<TYCD, int> callback, bool everyCallback, int repeatCount,int delay = 0, float duration = 1) { this.callback = callback; this.everyCallback = everyCallback; this.repeatCount = repeatCount; this.delay = delay; this.duration = duration; this.playing = true; } /// <summary> /// 重新设置duration每次执行时间 /// </summary> /// <param name="duration"></param> public virtual void ResetDuration(float duration) { if (this.duration != duration) { this.duration = duration; } } /// <summary> /// 开始 /// </summary> public virtual void StartCD() { if (!this.playing) return; this.DoCD(); } /// <summary> /// 处理倒计时 /// </summary> protected abstract void DoCD(); /// <summary> /// 暂停 /// </summary> public void Suspend() { this.playing = false; } /// <summary> /// 恢复 /// </summary> public void Recovery() { this.playing = true; } /// <summary> /// 停止 /// </summary> public virtual void Stop() { this.Suspend(); this.doCount = 0; } /// <summary> /// 销毁 /// </summary> public virtual void Dispose() { this.callback = null; } } public enum TYCD { /// <summary> /// 每次执行 /// </summary> Every = 1, /// <summary> /// 结束执行 /// </summary> Over = 2 } }
2⃣️,TimeCD
using System; using UnityEngine; namespace lib { public sealed class TimeCD : BaseCD { private float nextDuration; public TimeCD(Action<TYCD, int> callback, bool everyCallback, int repeatCount,int delay = 0, int duration = 1) : base( callback, everyCallback, repeatCount ,delay, duration) { } public override void Reset(Action<TYCD, int> callback, bool everyCallback, int repeatCount, int delay = 0, float duration = 1) { base.Reset(callback, everyCallback, repeatCount, delay, duration); this.nextDuration = this.duration; if (this.delay > 0) { this.nextDuration = Time.time + this.delay; } } public override void ResetDuration(float duration) { base.ResetDuration(duration); } protected override void DoCD() { if (Time.time >= this.nextDuration) { this.doCount++; if (this.doCount < this.repeatCount) { if (this.everyCallback) { this.callback(TYCD.Every, this.repeatCount - this.doCount); } } else { this.callback(TYCD.Over, 0); this.Stop(); } this.nextDuration = Time.time + 1; } } } }
3⃣️, TimeDeltaCD
using System; using UnityEngine; namespace lib { public sealed class TimeDeltaCD : BaseCD { private float accumulation; public TimeDeltaCD(Action<TYCD, int> callback, bool everyCallback, int repeatCount, int delay = 0, int duration = 1):base(callback, everyCallback, repeatCount, delay, duration) { } public override void Reset(Action<TYCD, int> callback, bool everyCallback, int repeatCount, int delay = 0, float duration = 1) { base.Reset(callback, everyCallback, repeatCount, delay, duration); if (this.delay > 0) { this.accumulation = - ( this.delay - 1 ); } else { this.accumulation = 1; } } public override void ResetDuration(float duration) { base.ResetDuration(duration); } /// <summary> /// /// </summary> protected override void DoCD() { this.accumulation += Time.deltaTime; if (this.accumulation >= this.duration) { this.doCount++; if (this.doCount < this.repeatCount) { if (this.everyCallback) { this.callback(TYCD.Every, this.repeatCount - this.doCount); } } else { this.callback(TYCD.Over, 0); this.Stop(); } this.accumulation = 0; } } } }
二, MonoBehaviour的InvokeRepeating的方法。(不讲解)