From 4156839bd9c95ae273b2ed5fdb40b99dae3a23f1 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Thu, 12 Mar 2026 12:41:22 +0800 Subject: [PATCH] =?UTF-8?q?docs(godot):=20=E6=9B=B4=E6=96=B0=E5=8D=8F?= =?UTF-8?q?=E7=A8=8B=E7=B3=BB=E7=BB=9F=E6=96=87=E6=A1=A3=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构协程系统架构说明,明确核心组件和入口点 - 更新协程启动方式和基本用法示例代码 - 添加 Segment 分段调度机制详细说明 - 补充节点生命周期绑定和延迟调用功能介绍 - 完善常用等待指令和协程控制方法文档 - 优化事件总线集成和上下文感知集成说明 --- docs/zh-CN/godot/coroutine.md | 565 +++++++++++++--------------------- 1 file changed, 217 insertions(+), 348 deletions(-) diff --git a/docs/zh-CN/godot/coroutine.md b/docs/zh-CN/godot/coroutine.md index 5970bb1..80c6314 100644 --- a/docs/zh-CN/godot/coroutine.md +++ b/docs/zh-CN/godot/coroutine.md @@ -1,406 +1,275 @@ # Godot 协程系统 -> GFramework 在 Godot 引擎中的协程支持,实现异步操作的优雅管理 - ## 概述 -GFramework.Godot 提供了与 Godot 引擎深度集成的协程系统,让异步编程变得简单直观。通过协程,您可以暂停执行、等待条件满足、或延迟执行操作,而不会阻塞主线程。 +GFramework 的协程系统由两层组成: -## 核心特性 +- `GFramework.Core.Coroutine` 提供通用调度器、`IYieldInstruction` 和一组等待指令。 +- `GFramework.Godot.Coroutine` 提供 Godot 环境下的运行入口、分段调度以及节点生命周期辅助方法。 -- **无缝集成**:与 Godot 的 `_Process`、`_Ready` 等生命周期方法完美配合 -- **类型安全**:强类型的协程返回结果处理 -- **自动清理**:协程与节点生命周期自动绑定,避免内存泄漏 -- **丰富的等待条件**:支持等待信号、时间延迟、帧结束等多种条件 +Godot 集成层的核心入口包括: + +- `RunCoroutine(...)` +- `Timing.RunGameCoroutine(...)` +- `Timing.RunUiCoroutine(...)` +- `Timing.CallDelayed(...)` +- `CancelWith(...)` + +协程本身使用 `IEnumerator`。 + +## 主要能力 + +- 在 Godot 中按不同更新阶段运行协程 +- 等待时间、帧、条件、Task 和事件总线事件 +- 显式将协程与一个或多个 `Node` 的生命周期绑定 +- 通过 `CoroutineHandle` 暂停、恢复、终止协程 +- 将命令、查询、发布操作直接包装为协程运行 ## 基本用法 -### 创建协程 - -使用 `StartCoroutine` 方法启动协程: +### 启动协程 ```csharp +using System.Collections.Generic; +using GFramework.Core.Abstractions.Coroutine; +using GFramework.Core.Coroutine.Instructions; using GFramework.Godot.Coroutine; +using Godot; -[ContextAware] public partial class MyNode : Node { public override void _Ready() { - // 启动协程 - this.StartCoroutine(DoSomethingAsync()); + Demo().RunCoroutine(); } - private System.Collections.IEnumerator DoSomethingAsync() + private IEnumerator Demo() { GD.Print("开始执行"); - - // 等待 2 秒 - yield return new WaitForSeconds(2.0f); - + + yield return new Delay(2.0); GD.Print("2 秒后继续执行"); - - // 等待下一帧 + yield return new WaitForEndOfFrame(); - - GD.Print("下一帧继续"); + GD.Print("当前帧结束后继续执行"); } } ``` -### 等待信号 +`RunCoroutine()` 默认在 `Segment.Process` 上运行,也就是普通帧更新阶段。 -协程可以等待 Godot 信号: +除了枚举器扩展方法,也可以直接使用 `Timing` 的静态入口: ```csharp -private System.Collections.IEnumerator WaitForSignalExample() -{ - GD.Print("等待按钮点击"); - - // 等待按钮被点击 - var button = GetNode