Skip to content

Yielders

Luke Mirman edited this page Sep 5, 2022 · 1 revision

Yielders Class

The Yielder class caches some commonly used Unity Yield Instructions to save memory in applications that very frequently use Coroutines.

In most coroutines you would have a statement such as yield return new WaitForSeconds(1.5f); or yield return new WaitForFixedUpdate(). As the new keyword would lead you to believe this creates a new object in memory which allocates a very minor amount of garbage that the application has to spend time cleaning up later. If you are very frequently using such yield statements across many scripts what was a trivial amount of garbage memory can start to add up to a significant allocation. To alleviate this the Yielders class caches the yield instructions so they can reused without creating new instances every time.

Yield Instructions

Original Syntax Yielders Syntax Information
yield return new WaitForFixedUpdate(); yield return Yielders.WaitForFixedUpdate; Waits until the next fixed frame rate update function.
See: WaitForFixedUpdate
yield return new WaitForEndOfFrame(); yield return Yielders.WaitForEndOfFrame; Waits until the end of the frame after Unity has rendered every Camera and GUI, just before displaying the frame on screen.
See: WaitForEndOfFrame
yield return new WaitForSeconds(1.5f); yield return Yielders.WaitForSeconds(1.5); Suspends the coroutine execution for the given amount of seconds using scaled time.
See: WaitForSeconds
yield return new WaitForSecondsRealtime(1.5f); yield return Yielders.WaitForSecondsRealtime(1.5f); Suspends the coroutine execution for the given amount of seconds using unscaled time.
See: WaitForSecondsRealtime
Clone this wiki locally