Yield

Static utility class providing frame-synchronous awaitable methods for async/await code running on Unity's main thread. Acts as the primary API for yielding execution back to the ParagonSynchronizationContext, resuming at specific Unity update phases. Also provides Task composition helpers (WhenAll, WhenAny) and cancellation integration via OnBeforeYield / OnAfterYield events.

Definition

Namespace: Paragon.Core.Async Assembly: Paragon.dll

public static class Yield

Remarks

Yield is the primary way game code interacts with the ParagonSynchronizationContext. Instead of using Task.Delay() or Task.Yield() (which do not integrate with Unity's frame loop or the cancellation system), game code uses await Yield.WaitForUpdate(), await Yield.WaitForSeconds(), etc.

Each WaitFor* method:

  1. Fires OnBeforeYield — used by CancellableTask to check for cancellation at every yield point.

  2. Awaits the appropriate YieldAwaitable — posts the continuation to the ParagonSynchronizationContext for execution at the correct update phase.

  3. Fires OnAfterYield — signals that the continuation has resumed.

This event pair is what makes the Action System's CancellableTask work: it hooks OnBeforeYield to call ThrowIfCancellationRequested(), ensuring that every yield point is a potential cancellation checkpoint.

Quick Lookup

Goal
How

Wait one frame (Update)

await Yield.WaitForUpdate()

Wait one frame (LateUpdate)

await Yield.WaitForEndOfFrame()

Wait one frame (FixedUpdate)

await Yield.WaitForFixedUpdate()

Wait N seconds (game time)

await Yield.WaitForSeconds(2f)

Wait until condition is true

await Yield.WaitUntil(() => isReady)

Wait while condition is true

await Yield.WaitWhile(() => isBusy)

Wait one editor frame

await Yield.WaitForEditorUpdate() (Editor only)

Wait N seconds (editor time)

await Yield.WaitForEditorSeconds(2f) (Editor only)

Already-completed task

Yield.CompletedTask

Wait for all tasks

await Yield.WhenAll(task1, task2)

Wait for any task

await Yield.WhenAny(task1, task2)

Properties

CompletedTask

Returns a pre-completed Task. Convenience wrapper around Task.CompletedTask.

Events

OnBeforeYield

Fired immediately before each yield point. Used by CancellableTask to throw OperationCanceledException if cancellation was requested.

OnAfterYield

Fired immediately after the continuation resumes from a yield point.

Methods

WaitForUpdate

Yields until the next Update phase.

WaitForFixedUpdate

Yields until the next FixedUpdate phase.

WaitForEndOfFrame

Yields until the next LateUpdate phase.

WaitForEditorUpdate

(Editor only) Yields until the next editor update tick.

WaitForSeconds

Yields across multiple frames until the specified duration (in game time) has elapsed.

Parameter
Type
Description

seconds

float

Duration to wait in seconds (uses Time.deltaTime)

circle-info

Uses Time.deltaTime, so it respects Time.timeScale. Each frame is a cancellation-checked yield point.

WaitForEditorSeconds

(Editor only) Yields across multiple editor ticks until the specified duration (in real editor time) has elapsed.

Parameter
Type
Description

seconds

float

Duration to wait in seconds (uses EditorApplication.timeSinceStartup)

WaitWhile

Yields each frame while the predicate returns true. Resumes on the first frame the predicate returns false.

Parameter
Type
Description

predicate

Func<bool>

Condition to evaluate each frame

executionTime

WorkExecutionTime

Which update phase to yield at. Default: UPDATE

WaitUntil

Yields each frame until the predicate returns true. Resumes on the first frame the predicate returns true.

Parameter
Type
Description

predicate

Func<bool>

Condition to evaluate each frame

executionTime

WorkExecutionTime

Which update phase to yield at. Default: UPDATE

WhenAll

Waits for all provided tasks to complete. Delegates to Task.WhenAll.

WhenAny

Waits for any one of the provided tasks to complete. Delegates to Task.WhenAny.

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation
circle-exclamation

Examples

Basic Frame Yielding

Timed Action

Conditional Waiting

Parallel Tasks

See Also

Last updated