Performance: Loading screens with coroutines
This concept affects the smoothness of loading screens and the responsiveness of the game during asset loading.
IEnumerator LoadGameCoroutine() {
yield return LoadAssetsAsync(); // Loads assets over multiple frames
ShowGameScreen();
}
void StartLoading() {
StartCoroutine(LoadGameCoroutine());
}void LoadGame() {
// Blocking load
LoadAllAssets(); // This blocks main thread
ShowGameScreen();
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Blocking load on main thread | N/A | N/A | Blocks frame rendering causing jank | [X] Bad |
| Coroutine-based async loading | N/A | N/A | Allows smooth frame updates during load | [OK] Good |
LoadSceneAsync in Unity?IEnumerator LoadScene()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("GameScene");
while (!asyncLoad.isDone)
{
Debug.Log($"Loading progress: {asyncLoad.progress * 100}%");
yield return null;
}
Debug.Log("Loading complete!");
}IEnumerator LoadLevel()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Level1");
while (asyncLoad.isDone == false)
{
progressBar.fillAmount = asyncLoad.progress;
yield return new WaitForSeconds(1);
}
}Pass the test and this pattern moves unknown