Performance: Coroutine basics (IEnumerator)
This concept affects how smoothly animations and timed events run without blocking the main game loop.
IEnumerator WaitThreeSeconds() {
yield return new WaitForSeconds(3f);
Debug.Log("3 seconds passed");
}
void Start() {
StartCoroutine(WaitThreeSeconds());
}void Update() {
float timer = 0f;
while (timer < 3f) {
timer += Time.deltaTime;
}
Debug.Log("3 seconds passed");
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Blocking loop in Update | N/A | Blocks main thread | Freezes frame rendering | [X] Bad |
| Coroutine with yield return | N/A | Non-blocking | Smooth frame rendering | [OK] Good |
Coroutine in Unity?IEnumerator PrintNumbers() {
for (int i = 1; i <= 3; i++) {
Debug.Log(i);
yield return new WaitForSeconds(1);
}
}
StartCoroutine(PrintNumbers());IEnumerator Example() {
Debug.Log("Start");
yield WaitForSeconds(2);
Debug.Log("End");
}
StartCoroutine(Example());IEnumerator and yield return to achieve this?null each frame until 3 seconds pass uses a loop with yield return null to wait one frame each iteration, allowing smooth fade over 3 seconds.Pass the test and this pattern moves unknown