Performance: WaitForSeconds and WaitForEndOfFrame
This affects frame rendering timing and responsiveness in Unity games or apps.
IEnumerator SmoothUpdate() {
while(true) {
yield return new WaitForEndOfFrame();
// lightweight logic here
}
}IEnumerator SlowUpdate() {
while(true) {
yield return new WaitForSeconds(0.5f);
// heavy logic here
}
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| WaitForSeconds with long delay | N/A | N/A | Delays frame updates causing lower responsiveness | [X] Bad |
| WaitForEndOfFrame | N/A | N/A | Syncs with frame end, smooth updates | [OK] Good |
WaitForSeconds do in a Unity coroutine?new WaitForEndOfFrame().IEnumerator Example() {
Debug.Log("Start");
yield return new WaitForEndOfFrame();
Debug.Log("End of Frame");
yield return new WaitForSeconds(1f);
Debug.Log("After 1 second");
}IEnumerator WaitExample() {
Debug.Log("Before wait");
yield WaitForSeconds(2f);
Debug.Log("After wait");
}Pass the test and this pattern moves unknown