From 7314bf03d0742414b94a0f5a72bd95c25abad095 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Fri, 23 Jan 2026 12:50:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(coroutine):=20=E6=B7=BB=E5=8A=A0=E5=9F=BA?= =?UTF-8?q?=E4=BA=8E=E6=9D=A1=E4=BB=B6=E5=92=8C=E5=8F=96=E6=B6=88=E4=BB=A4?= =?UTF-8?q?=E7=89=8C=E7=9A=84=E9=87=8D=E5=A4=8D=E8=B0=83=E7=94=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增RepeatCallForever方法支持条件函数参数 - 添加CancellationToken参数支持取消令牌控制 - 重构原有RepeatCallForever方法保持向后兼容 - 添加详细的XML文档注释说明参数和返回值 - 实现基于条件判断的循环执行逻辑 - 实现基于取消令牌的循环执行控制 --- GFramework.Core/coroutine/CoroutineHelper.cs | 34 +++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/GFramework.Core/coroutine/CoroutineHelper.cs b/GFramework.Core/coroutine/CoroutineHelper.cs index b6054b9..938fa92 100644 --- a/GFramework.Core/coroutine/CoroutineHelper.cs +++ b/GFramework.Core/coroutine/CoroutineHelper.cs @@ -83,16 +83,42 @@ public static class CoroutineHelper yield return new Delay(interval); } } - + /// - /// 无限重复调用指定的委托 + /// 无限重复调用指定的委托,直到条件不满足 /// /// 每次调用之间的间隔时间(秒) /// 要执行的动作委托 + /// 继续执行的条件,返回false时停止 /// 返回一个枚举器,用于协程执行 - public static IEnumerator RepeatCallForever(double interval, Action? action) + public static IEnumerator RepeatCallForever( + double interval, + Action? action, + Func? shouldContinue = null) { - while (true) + // 循环执行动作直到条件不再满足 + while (shouldContinue?.Invoke() ?? true) + { + action?.Invoke(); + yield return new Delay(interval); + } + } + + /// + /// 无限重复调用指定的委托,直到取消令牌被触发 + /// + /// 每次调用之间的间隔时间(秒) + /// 要执行的动作委托 + /// 用于控制循环执行的取消令牌 + /// 返回一个枚举器,用于协程执行 + public static IEnumerator RepeatCallForever( + double interval, + Action? action, + CancellationToken token + ) + { + // 循环执行动作直到取消令牌被请求取消 + while (!token.IsCancellationRequested) { action?.Invoke(); yield return new Delay(interval);