using System.Collections; using GFramework.Game.Abstractions.coroutine; namespace GFramework.Game.coroutine; /// /// 为协程作用域提供扩展方法,支持延迟执行和重复执行功能 /// public static class CoroutineScopeExtensions { /// /// 启动一个延迟执行的协程 /// /// 协程作用域 /// 延迟时间(秒) /// 延迟后要执行的动作 /// 协程句柄,可用于控制协程的生命周期 public static CoroutineHandle LaunchDelayed(this ICoroutineScope scope, float delay, Action action) => ((CoroutineScope)scope).Launch(DelayedRoutine(delay, action)); /// /// 创建延迟执行的协程迭代器 /// /// 延迟时间(秒) /// 要执行的动作 /// 协程迭代器 private static IEnumerator DelayedRoutine(float delay, Action? action) { yield return new WaitForSeconds(delay); action?.Invoke(); } /// /// 启动一个重复执行的协程 /// /// 协程作用域 /// 重复间隔时间(秒) /// 每次重复时要执行的动作 /// 协程句柄,可用于控制协程的生命周期 public static CoroutineHandle LaunchRepeating(this ICoroutineScope scope, float interval, Action action) => ((CoroutineScope)scope).Launch(RepeatingRoutine(interval, action)); /// /// 创建重复执行的协程迭代器 /// /// 重复间隔时间(秒) /// 要执行的动作 /// 协程迭代器 private static IEnumerator RepeatingRoutine(float interval, Action action) { while (true) { action?.Invoke(); yield return new WaitForSeconds(interval); } } }