Skip to content

WaitFor Utility Class

Andrzej Kebab edited this page Feb 4, 2024 · 1 revision

UtilityLibrary.Unity.Runtime

The WaitFor class in the UtilityLibrary.Unity.Runtime namespace provides cached instances of Unity's WaitForFixedUpdate, WaitForEndOfFrame, WaitForSeconds, and WaitForSecondsRealtime. This utility class helps reduce memory allocation by reusing instances for commonly used wait periods.

WaitFor Class

Properties

  • ForFixedUpdate: A cached instance of WaitForFixedUpdate.
  • ForEndOfFrame: A cached instance of WaitForEndOfFrame.
  • ForSeconds: A cached instance of WaitForSecondsCache, allowing access to WaitForSeconds with cached durations.
  • ForSecondsRealtime: A cached instance of WaitForSecondsRealtimeCache, allowing access to WaitForSecondsRealtime with cached durations.

Nested Classes

WaitForSecondsCache

  • Properties
    • Item[float time]: Gets a cached instance of WaitForSeconds with the specified duration.

WaitForSecondsRealtimeCache

  • Properties
    • Item[float time]: Gets a cached instance of WaitForSecondsRealtime with the specified duration.

Usage Example

// Use cached instances of WaitForSeconds and WaitForSecondsRealtime
IEnumerator Start()
{
    Debug.Log("Start Coroutine");
    yield return WaitFor.ForSeconds[2.0f];
    Debug.Log("After 2 seconds");

    yield return WaitFor.ForSecondsRealtime[3.0f];
    Debug.Log("After 3 seconds in real time");

    yield return WaitFor.ForFixedUpdate;
    Debug.Log("After the next FixedUpdate");

    yield return WaitFor.ForEndOfFrame;
    Debug.Log("After the end of the frame");
}

This utility class simplifies coroutine waiting in Unity by providing cached instances of commonly used WaitFor types, reducing memory allocation and enhancing performance.

Clone this wiki locally