Create a Loading Screen in Unity
We are going to create a scene that will load before the actual level which will be used to show the progress.
Let’s start with a simple bar. We are going to need an image…Let’s add an Image along with the Canvas:
After you have draged your image (I created a simple white rectangle), change the “Image Type” to “Filled” and the “Fill Method” to “Horizontal:
With your image selected in the Scene View, adjust the size to your liking:
As you can see, if you drag the “Fill Amount” slider to the left, you can see the bar shrink:
If you look at the values, you’ll notice that the amount ranges from 0 to 1. All that is left now is to get a reference to the fill amount and set it to the amount of the scene we need loaded.
With the code above, we create a Coroutine. Within the routine, we assign a variable of AsyncOperation to the value of the scene we want loaded.
Then within a while loop, we call the “isDone” method and set the value of the progress bar’s “fill amount” to the value of the AsyncOperations’s “progress”.
We finally end the process yielding a “WaitForEndOfFrame()” prevent the computer from crashing…giving it ‘time to breath’, so to speak.