Objective: Create a loading scene to load the game.
To start off on this adventure of loading a scene in Unity, we will need to create a new scene, let’s name it LoadingScene.
In this scene we will need to create three image objects, One to hold the background image, one to hold the progress bar and finally one to hold the overlay that covers the progress bar.
After we add in the correct images to it’s location, we’ll go to the progress bar. In the Image component, we need to set the image type to Filled. Filled method to horizontal and origin to left. This will allow us to fill the image to match the amount of progress that has been made.
Once this is all set it, it’s time to get into the logic. We will create a LoadLevel script and attach it to the canvas object.
Now that we are in the script, we need to create an variable to hold the progress bar object. But to use UI variables we need to add the library first. Since this will load the main game, we might as well add SceneManagement as well now.
We need a variable to hold the Image object for the progress bar.
Now we are gonna create an IEnumerator that’ll hand the loading of the next scene. We will need to create a AsyncOperation and assign it the SceneManager.LoadSceneAsync(“Pass in the scene name you want to load”);. Next we will need to use a while loop to run while the asyncLoad is not done loading. Inside the while loop, we will use the image object and set the fillamount to equal the asyncLoad.progress. We do need to let the program breath so we will yield return new WaitForEndOfFrame();
In the start function we need to start the CoRoutine.
All that’s left to do is in the main menu change the loadscene to loadLevel and not the main game scene.
To learn more about AsyncOperation head off to here and learn a little bit more. https://docs.unity3d.com/ScriptReference/AsyncOperation.html
Thank you for reading and have a wonderful day!