本文由 AI 分析生成
建立時間: 2026-03-28 來源: https://community.gamedev.tv/t/yield-return-is-breaking-my-brain/225304
Summary
A forum discussion on GameDev.tv where a developer struggles to understand how yield return works in Unity coroutines, specifically when a coroutine yields another coroutine. The question explores the control flow: when FadeOut yields a coroutine, does Unity jump into that coroutine, and how does yield return null cause repeated execution?
這是 GameDev.tv 論壇上的一篇討論,開發者對 Unity 協程中 yield return 的控制流感到困惑,特別是當一個協程 yield 另一個協程時 Unity 的執行邏輯,以及 yield return null 如何導致重複執行。
Key Points
- Unity coroutines use
yield returnto pause execution and return control to Unity’s scheduler - Yielding a coroutine (
yield return someCoroutine) causes Unity to execute that coroutine to completion before resuming the parent yield return nullpauses for one frame and resumes on the next update cycle- Stopping a currently running coroutine and starting a new one prevents overlapping animations
- The behavior differs from async/await in that Unity manually drives the coroutine scheduler each frame
Insights
The confusion arises from a mental model mismatch: developers familiar with async/await expect bidirectional suspension, but Unity coroutines are more like cooperative multitasking driven by a frame clock. The key insight is that yield return null doesn’t end the coroutine — it yields for exactly one frame, then resumes from exactly that point. This is a common stumbling block for developers moving from traditional async to Unity’s coroutine system.
Connections
Raw Excerpt
What doesn’t make sense is what happens when the unity back end goes through and continues the coroutines. We’ve returned a coroutine, but FadeOut was itself a coroutine that was called by the scene management somewhere.