using System.Collections; using GFramework.Game.Abstractions.coroutine; namespace GFramework.Game.coroutine; /// /// 为ICoroutineScope提供扩展方法,支持延迟执行和重复执行协程功能 /// public static class CoroutineScopeExtensions { /// /// 在指定延迟时间后启动一个协程来执行给定的动作 /// /// 协程作用域 /// 延迟时间(秒) /// 要执行的动作 /// 协程句柄,可用于控制或停止协程 public static ICoroutineHandle LaunchDelayed(this ICoroutineScope scope, float delay, Action action) { return scope.Launch(DelayedRoutine(delay, action)); } /// /// 创建一个延迟执行的协程例程 /// /// 延迟时间(秒) /// 要执行的动作,可为空 /// 协程迭代器 private static IEnumerator DelayedRoutine(float delay, Action? action) { yield return new WaitForSeconds(delay); action?.Invoke(); } /// /// 启动一个重复执行的协程,按照指定间隔时间循环执行给定动作 /// /// 协程作用域 /// 执行间隔时间(秒) /// 要重复执行的动作 /// 协程句柄,可用于控制或停止协程 public static ICoroutineHandle LaunchRepeating(this ICoroutineScope scope, float interval, Action action) { return scope.Launch(RepeatingRoutine(interval, action)); } /// /// 创建一个重复执行的协程例程 /// /// 执行间隔时间(秒) /// 要重复执行的动作 /// 协程迭代器 private static IEnumerator RepeatingRoutine(float interval, Action action) { // 持续循环执行动作并等待指定间隔 while (true) { action?.Invoke(); yield return new WaitForSeconds(interval); } } }