0
0
Unityframework~10 mins

Loading screens with coroutines in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Loading screens with coroutines
Start Loading Screen
Start Coroutine
Begin Async Load
Check Load Progress
Update UI
Hide Loading Screen
The loading screen starts, a coroutine runs to load the scene asynchronously, updating progress until complete, then hides the loading screen.
Execution Sample
Unity
IEnumerator LoadSceneAsync()
{
  loadingScreen.SetActive(true);
  AsyncOperation op = SceneManager.LoadSceneAsync("GameScene");
  while (!op.isDone)
  {
    progressBar.fillAmount = op.progress;
    yield return null;
  }
  loadingScreen.SetActive(false);
}
This coroutine shows a loading screen, loads a scene in the background, updates a progress bar, and hides the screen when done.
Execution Table
StepActionAsyncOperation.isDoneAsyncOperation.progressUI UpdateYield
1Activate loading screenFalse0.0Show loading screenYield null
2Start loading scene asyncFalse0.1Update progress bar to 10%Yield null
3Check loading progressFalse0.4Update progress bar to 40%Yield null
4Check loading progressFalse0.7Update progress bar to 70%Yield null
5Check loading progressFalse0.9Update progress bar to 90%Yield null
6Check loading progressTrue1.0Update progress bar to 100%Exit loop
7Deactivate loading screenTrue1.0Hide loading screenEnd coroutine
💡 AsyncOperation.isDone becomes True, loading completes, coroutine ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
AsyncOperation.isDoneFalseFalseFalseFalseFalseTrueTrue
AsyncOperation.progress0.00.10.40.70.91.01.0
loadingScreen.activeSelfFalseTrueTrueTrueTrueTrueFalse
progressBar.fillAmount0.00.10.40.70.91.01.0
Key Moments - 3 Insights
Why does the coroutine use 'yield return null' inside the while loop?
Because 'yield return null' pauses the coroutine until the next frame, allowing the loading progress to update smoothly each frame as shown in steps 2 to 5.
Why is the loading screen active before starting the async load?
Activating the loading screen first (step 1) ensures the user sees the loading UI immediately while the scene loads in the background, as seen in the execution_table.
What causes the while loop to exit?
The loop exits when AsyncOperation.isDone becomes True (step 6), meaning the scene has finished loading.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is AsyncOperation.progress at step 4?
A0.9
B0.4
C0.7
D1.0
💡 Hint
Check the 'AsyncOperation.progress' column at step 4 in the execution_table.
At which step does the loading screen get hidden?
AStep 6
BStep 7
CStep 5
DStep 1
💡 Hint
Look at the 'UI Update' column to find when the loading screen is hidden.
If 'yield return null' was removed from the loop, what would happen?
AThe coroutine would complete instantly without waiting.
BThe progress bar updates smoothly each frame.
CThe loading screen would never activate.
DThe coroutine would freeze and never update progress.
💡 Hint
Refer to the 'key_moments' explanation about 'yield return null' and the execution_table loop steps.
Concept Snapshot
Loading screens with coroutines in Unity:
- Activate loading UI
- Start async scene load with SceneManager.LoadSceneAsync
- Use a coroutine with while(!op.isDone) loop
- Update progress bar each frame with op.progress
- Use 'yield return null' to wait a frame
- Hide loading UI when loading completes
Full Transcript
This example shows how to use a coroutine in Unity to display a loading screen while a scene loads asynchronously. The coroutine activates the loading screen, starts loading the scene in the background, and repeatedly updates a progress bar based on the loading progress. It uses 'yield return null' inside a while loop to wait for the next frame, allowing the UI to update smoothly. When the loading finishes, the coroutine hides the loading screen and ends. The execution table traces each step, showing the AsyncOperation's progress and completion status, UI updates, and coroutine yields. Key moments clarify why the coroutine yields each frame and when the loading screen is shown and hidden. The visual quiz tests understanding of progress values, UI changes, and coroutine behavior.