using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; namespace WS { [ObjectSystem] public class TimerComponentAwakeSystem : AwakeSystem { public override void Awake(TimerComponent self) { self.Awake(); } } [ObjectSystem] public class TimerComponentUpdateSystem : UpdateSystem { public override void Update(TimerComponent self) { self.Update(); } } ///时间管理器 public class TimerComponent : SingleComponent { ///所有时间实体 private List ListTimes = new List(); ///协程载体 private Init Mono; ///是否暂停 public bool IsPause; public void Awake() { Mono = GameObject.FindObjectOfType(); } public void Update() { if (!IsPause) { for (int i = ListTimes.Count - 1; i >= 0; i--) { if (!ListTimes[i].IsPause) { ListTimes[i].UseTime += Time.deltaTime; ListTimes[i].OnUpdate?.Invoke(ListTimes[i].UseTime); if (ListTimes[i].UseTime >= ListTimes[i].WaitTime && !ListTimes[i].IsPause) { if (ListTimes[i].Tcs == null) { ListTimes[i].OnCallback?.Invoke(); if (ListTimes[i].IsLoop) { ListTimes[i].UseTime = 0; } else { ListTimes[i].Dispose(); ListTimes.Remove(ListTimes[i]); } } else { ListTimes[i].Tcs.SetResult(true); ListTimes[i].Dispose(); ListTimes.Remove(ListTimes[i]); } } } } } } ///异步时间等待器 public Task WaitAsync(float time) { TaskCompletionSource tcs = new TaskCompletionSource(); TimerEntity entity = ComponentFactory.CreateWithParent(this); entity.WaitTime = time; entity.Tcs = tcs; ListTimes.Add(entity); return tcs.Task; } /// 创建时间等待器(是暂停状态需要手动开始) /// 等待时间 /// 时间到后的回调 /// 帧逻辑 /// 循环 public TimerEntity Create(float time, Action oncallback = null, bool loop = false, Action onupdate = null) { TimerEntity entity = ComponentFactory.CreateWithParent(this); entity.WaitTime = time; entity.OnCallback = oncallback; entity.OnUpdate = onupdate; entity.IsLoop = loop; entity.IsPause = true; ListTimes.Add(entity); return entity; } /// 创建时间等待器(创建并执行) /// 等待时间 /// 时间到后的回调 public TimerEntity Wait(float time, Action oncallback) { return Wait(time, oncallback, false, null); } /// 创建时间等待器(创建并执行) /// 等待时间 /// 时间到后的回调 /// 循环 public TimerEntity Wait(float time, Action oncallback, bool loop) { return Wait(time, oncallback, loop, null); } /// 创建时间等待器(创建并执行) /// 等待时间 /// 时间到后的回调 /// 帧逻辑 /// 循环 public TimerEntity Wait(float time, Action oncallback = null, bool loop = false, Action onupdate = null) { TimerEntity entity = ComponentFactory.CreateWithParent(this); entity.WaitTime = time; entity.OnCallback = oncallback; entity.OnUpdate = onupdate; entity.IsLoop = loop; ListTimes.Add(entity); return entity; } ///停止并销毁 public void Stop(TimerEntity entity) { if (ListTimes.Exists(t => t == entity)) { entity.IsPause = true; entity.Dispose(); ListTimes.Remove(entity); } } ///开启Mono协程(不受IsPause控制) public Coroutine StartCoroutine(IEnumerator routine) { return Mono.StartCoroutine(routine); } ///停止Mono协程 public void StopCoroutine(IEnumerator routine) { Mono.StopCoroutine(routine); } public override void Dispose() { base.Dispose(); IsPause = false; for (int i = 0; i < ListTimes.Count; i++) { ListTimes[i].Dispose(); } ListTimes.Clear(); Mono.StopAllCoroutines(); Mono = null; } } }