Sometimes it can take several seconds to load large scenes in Unity. When this is the case, it is a good practice to use a progress bar to give the user a good idea how much loading time is left. This can be implemented using Unity’s Asynchronous operations.
When I click on the start button above, the scene will change to a loading scene that will display a progress bar. The progress bar will fill as the next scene loads. When the next scene is fully loaded it will activate immediately.
In order to accomplish this, the first thing we do is switch to a loading scene that will display our progress bar:
As the next scene is loading asynchronously, we will update the progress bar every frame:
Our progress bar is a basic UI image with its image type set to Filled, its fill method set to Horizontal, and fill origin set to Left. The fill amount will be updated in code as the operation completes.
The code that accomplishes this is basic and straightforward:
When the loading scene begins, the LoadMainScene coroutine is started. In this routine we declare an AsynchOperation and set it equal to LoadSceneAsync(sceneToLoad).
The While loop will update the progress bar’s fill amount each frame. The fill amount is a float from 0 to 1, which is exactly what the asynchOperation.progress value is. When the next scene is fully loaded, the progress will be 1 with a full progress bar. The next scene will then begin automatically once it is fully loaded.
That’s all it takes to implement a loading progress bar in Unity.