---
title: Yield 指令参考
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/scripting/object-oriented-development/coroutines-section/coroutines-yield-instructions
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/scripting/object-oriented-development/coroutines-section/coroutines-yield-instructions.md
generated_at: 2026-11-16T06:44:17.118Z
locale: zh-cn
---

# Yield 指令参考

> 在协程方法中产生不同的自定义定义指令来Control恢复When。

协程在 `yield return` 语句中暂停执行。`yield return null` 将暂停协程的执行，直到下一帧。但是，`yield return`还可以返回 Unity Editor 或运行时的指令，例如等待指定时间量或直到满足某个条件，然后恢复协程的执行。

## 运行时 yield 指令##runtime-yield-instructions

Unity 有一组派生自[`UnityEngine.YieldInstruction`的](/engine/6000.0/script-reference/unityengine/yieldinstruction.md)自定义产量指令，您可以使用这些指令在指定时间之后、满足指定条件时或在 Player Loop 中的指定点恢复。

| 说明                                                                                                | 描述                                                                                                                                 |
| ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| [`AsyncOperation`](/engine/6000.0/script-reference/unityengine/asyncoperation.md)                 | 暂停协程，并在异步操作完成（例如加载中Scene或资源）When恢复。                                                                                                |
| [`WaitForEndOfFrame`](/engine/6000.0/script-reference/unityengine/waitforendofframe.md)           | 暂停协程，并在All渲染和 GUI 事件之后在帧结束恢复。**注意**：当 Editor 处于批次模式时，`WaitForEndOfFrame` 绝不会以编辑模式运行，即使脚本标有`[ExecuteInEditMode]`或`[ExecuteAlways]`。 |
| [`WaitForFixedUpdate`](/engine/6000.0/script-reference/unityengine/waitforfixedupdate.md)         | 暂停协程，并在 Next Physics 更新结束时在 All Physics 计算之后继续。                                                                                    |
| [`WaitForSeconds`](/engine/6000.0/script-reference/unityengine/waitforseconds.md)                 | 暂停协程并在指定秒数量恢复，同时帐户时间尺度。                                                                                                            |
| [`WaitForSecondsRealtime`](/engine/6000.0/script-reference/unityengine/waitforsecondsrealtime.md) | 暂停协程并在指定数量的秒后恢复，忽略时间尺度。                                                                                                            |
| [`WaitUntil`](/engine/6000.0/script-reference/unityengine/waituntil.md)                           | 暂停协程，当提供的委托评估到 `true` 时恢复。                                                                                                         |
| [`WaitWhile`](/engine/6000.0/script-reference/unityengine/waitwhile.md)                           | 暂停协程，当提供的委托评估到 `false` 时恢复。                                                                                                        |

有关这些 yield 指令的更多信息和示例用法，请参阅其 API 参考面。

有关在 Player Loop 中恢复不同协程的位置的直观表示，请参阅[事件函数执行 Order](/engine/6000.0/manual/scripting/object-oriented-development/managing-update-order/execution-order.md) 中的图表。

## UnityTest yield 说明##unitytest-yield-instructions

标记为 `[UnityTest]` 属性的 Unity Test Framework 测试作为协程运行。Test Framework 包添加了对其他 yield 指令的Support，用于Control Unity Editor 的测试，并提供了定义自定义定义 yield 指令的可能性。

| 说明                                                                                                                                                                  | 描述                                 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------- |
| [`EnterPlayMode`](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/api/UnityEngine.TestTools.EnterPlayMode.html)             | 为 Unity Editor 创建 yield 指令以进入播放模式。 |
| [`ExitPlayMode`](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/api/UnityEngine.TestTools.ExitPlayMode.html)               | 为 Unity Editor 创建退出播放模式的 yield 指令。 |
| [`RecompileScripts`](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/api/UnityEngine.TestTools.RecompileScripts.html)       | 在 Unity Editor 中触脚本的重新编译。          |
| [`WaitForDomainReload`](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/api/UnityEngine.TestTools.WaitForDomainReload.html) | 将脚本的执行延迟到 Incoming 域重新加载之后。        |

有关 Unity Test Framework 提供的 yield 指令的更多信息，请参阅 [Yield 指令 Editor](/engine/6000.0/manual/reference-custom-yield-instructions.md)。

## Editor yield 指令##editor-yield-instructions

Editor Coroutines包添加了对在 Unity Editor 的 编辑模式下运行的协程的Support。包包含编辑模式协程的其他 yield 指令。

| 说明                                                                                                                                                                              | 描述                                  |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- |
| [`EditorWaitForSeconds`](https://docs.unity3d.com/Packages/com.unity.editorcoroutines@latest/index.html?subfolder=/api/Unity.EditorCoroutines.Editor.EditorWaitForSeconds.html) | 在指定秒数量恢复 EditorCoroutine，同时帐户到时间尺度。 |

有关更多信息，请参阅 [Editor Coroutines](https://docs.unity3d.com/Packages/com.unity.editorcoroutines@latest)。

## 批处理模式支持##batch-mode-support

当[以批次模式](/engine/6000.0/manual/unity-editor/command-line-arguments/player.md)运行独立平台播放器时，所有运行时协程 yield 指令均正常运行。

如果 [Editor 以批次模式](/engine/6000.0/manual/unity-editor/command-line-arguments/editor.md#batchmode)运行，并且 Project 具有标记为 [`[ExecuteInEditMode]`](/engine/6000.0/script-reference/unityengine/executeineditmode.md) 或 [`[ExecuteAlways]`](/engine/6000.0/script-reference/unityengine/executealways.md) 的脚本，因此它们也以编辑模式运行，则这些脚本中的所有运行时协程都以编辑模式运行，但 `WaitForEndOfFrame` 除外。这是因为并非All Unity 子系统都像在运行时时一样在编辑模式下定期更新。如需了解更多信息，请参阅 [`[ExecuteAlways]`](/engine/6000.0/script-reference/unityengine/executealways.md) API 参考。

## 其他资源##additional-resources

* [命令行参数](/engine/6000.0/manual/unity-editor/command-line-arguments.md)
* API 参考
