The only possible yield values the scheduler understands are:
- Classes derived from
YieldInstruction
(WaitForSeconds
, WaitForEndOfFrame
, WaitForFixedUpdate
, AssetBundleCreateRequest
, AssetBundleRequest
and Coroutine
- a
WWW
object
- "any other value" which isn't one of the above.
If "any other value" is yielded (which includes "null" a string value any other basic type such as int, bool, ... or a reference to an arbitrary object which isn't one of the above mentioned) the scheduler will "schedule" the coroutine for the next frame.
- Bunny83 answer: Prominent member on Unity Answers
The WaitForEndOfFrame
and others of the like, are just blank functions that tag the YieldInstruction
in order to decide what to do in the engine.
The default case seems to be WaitForEndOfFrame
. So if you yield return
something that doesn't have a special meaning, such as a bool, it is the same as WaitForEndOfFrame
.
There doesn't seem to be any official documentation on this behavior.
Update
rutter commented about another special case: yield return null
All of the Unity Coroutines including yield return null
, run before the frame renders except for WaitForEndOfFrame
. You can find rutter's awesome answer over at Unity Answers explaining this further (nice diagrams included).
yield return true
does something special. Butyield return null
simply pauses for a frame, so I suspect thatyield return false
has the exact same effect (alsoyield return 0
since 0 false null are all the same under the hood) \$\endgroup\$if (0)
orif (null)
, unlike in C and C++. \$\endgroup\$yield return 0
works; apparently because of the reason MLM explained \$\endgroup\$yield
keyword has been around since at least C# 2.0. All Unity does is call it coroutine and handles the generated iterator and the values returned from it in a way that you can cause non-blocking delays in the execution of your code. \$\endgroup\$WaitForSeconds
is not declared. As soon as you declare a method calledWaitForSeconds
that takes a float argument and that returns the element type of the enumeration you are implementing, it would compile again. Hence,yield
is not special to Unity. If anything, theWaitForSeconds
method is special to Unity, but even then, it does not extend the language C#, but is simply an extra method that was written using standard C#. \$\endgroup\$