Create a loading bar in Unity
Objective: Create a loading bar!
When we change scene’s, sometimes the scene will take a minute to load due to all the assets. We can use Scenemanager’s Load Scene Async to load the scene before switching scenes. Using this we can show progress with a slider.
To get started we will need a slider in the scene.
We will create another scene. It can be loaded with anything at the moment, the more assets the longer the load will take.
Once the scene is created we will need to drop it in the Build Settings.
With that, we will create a script for loading next scene. In here we will need to grab the UI and SceneManagement library.
Now we need to create a variable for the slider. I set the value of the slider to 0 in start.
Next, create an IEnumerator to have the async in. We will create a local variable and assign it to the Scenemanager.LoadSceneAsync and pass in the next scene name, you could also pass in the scene index found in build settings.
Create a While loop that will go until the asyncLoad is done. In here we will assign the slider value to the asyncLoad.progress. Progress returns a float. To finish this off we will yield return null;.
Finally, just call StartCoroutine and pass in the IEnumerator name to get it to load. I just use input to start it.
With that, we can create a loading bar that shows progress until the scene is loaded.
Thank you for reading and have a wonderful day!