Scene Manager - Load between scenes and show a progress bar - [ Unity Tutorial ]
Show a progress bar to your players as a new scene loads. Loading your scenes asynchronously can help reduce the time users have to wait between levels.
❤️ Become a Tarobro on Patreon: https://www.patreon.com/tarodev
🔔 SUBSCRIBE: https://bit.ly/3eqG1Z6
🗨️ DISCORD: https://discord.gg/GqeHHnhHpz
✅ MORE TUTORIALS: https://www.youtube.com/tarodev
Develop video games like a pro!
Whether you're a beginner or a seasoned game developer, you'll find lots of useful tips and tricks to boost your development career. Visit the channel: https://www.youtube.com/channel/UCAg2py6olXgod71Ix06HnUQ
good evening friends today i'm going to show you how to use the scene manager provided by unity and also hook up a loading screen with a progress bar so first thing we're going to need to do is create a new script and unity already has a class called scene manager which i ideally would have liked to call this class but instead i'm going to call it level manager just to avoid conflicts open that up let's make a public static level manager and we'll call that instance and now in our awake function we will say if instance equals null well we will make this instance equals this and then we will say don't destroy onload and we'll send in the game object so basically what that means is if the scene that we're currently in gets destroyed on the next scene it will still be there it won't be destroyed in between the scenes and if it is in fact already been uh declared and assigned to we will just destroy this version of the object just like that next we'll create a new function and we'll call this actually it will be an async function and we'll call this load scene and this will take in a same name as a parameter now we need to start loading our scene so we'll call this scene equals uh scene manager dot load scene async and we'll send in the scene name there next we'll be preventing the scene from activating automatically so uh we're not going to allow it to straightaway transition to the new scene as soon as it's loaded all right next we're going to grab a few references so this first one will be a game object and it will be my actual loader canvas object which will be the actual main object here which i will enable and disable and the second one will be this foreground sprite here which by the way is just a normal image the image type is set to filled horizontal starting from left to right and as you can see there that's that's the property that we'll be setting so back here another one private image and this will be my rigorous bar now down here in our load scene function first set uh loader canvas set active true so we'll be showing our loader and now we need to periodically check to see how much the scene is loaded and ultimately change that uh image fill amount to reflect how much has actually been loaded so i'll do that in a do while loop uh if you don't know what a do our loop is it's the same as a while loop the only difference is that the logic inside will run at least once and then once it gets down to this uh while condition here it will just act like a normal while loop after that so our loop condition that we're going to be watching for is just saying progress is less than 0.9 so don't ask me why unity does this but basically the scene is loaded at ninety percent not a hundred go figure who knows uh but make sure you're doing it two point nine and not one or else your scene will load forever and ever and ever and over and over so now for the purpose of this demo i'm assuming these scenes are going to load instantly which won't be very fun and you won't see anything so i'm just going to add an artificial wait time here make sure you don't add this line when you're creating your loader and now i'm going to set the progress bar bill amount equals to the scene dodge progress okay so i'm looping here uh every 0.1 seconds i'm going to update our progress fill amounts and once it's completely loaded we're going to continue here and then all you need to do to activate your scene again is you need to take this and you need to change it to true as soon as you tell unity that the scene can activate they'll activate it and you will be taken to the next scene next we will disable our loading canvas now that the new scene has loaded because you have to remember that this is a singleton so when the new scene loads this object will the very same object will still be present in the new scene and it will have the loaded canvas showing so we have to disable that next we actually need a way to trigger this function to load to the next scene so back in unity you'll see i've got these two buttons here go to scene two and then in the next scene i've got go to scene one and on my buttons i've just got a script here called change scene button and then in the button on click event oh my face is in the way let me just move over here in the button uh click event i've got i'm referencing this change scene button and sending in the scene that i'd like to go to and i'll just show you what that script actually looks like it's literally just a function with the same name parameter so in this i'm going to say level manager instance uh singleton instance and i'll say loadsene and send in a same name and that should actually be all we need to change these scenes here ah no actually we got something so on the level manager let's actually attach our level manager script and i'll send in my actual loading canvas objects and then a foreground of our progress bar all right now that's done we should be able to try this out so go to cn2 yep working as expected go back to scene one back to scene two there we go so uh this is not too exciting and obviously it's uh it completely fills up to ninety percent doesn't look too good uh if you'd like to show more of a progression what we can do is we can instead of setting our fill amount directly to the scene progress we can do it over time so let's create a reference a float and it will be our target now instead of setting the fill amount directly we'll set our target to that instead and then in a update function we'll say our fill amount equals to math f move towards we'll send in our current uh fill amount we'll say our target which we're trying to get to and i don't know three times time dot delta time uh and now also as this as we're reusing this scene manager in between the scenes uh every time that we show it we would like to reset uh filber wouldn't we or else it's just gonna always start at a hundred percent okay progress bar dot fill amount uh equal zero and we'll also set our target back to zero as well just like that let's try it out yeah there we go that looks more authentic so yeah obviously you could improve on this quite a lot you could for example add a fade in and out to the screen here to make it less jarring you could add some tips below the loading message or whatever you could add you could create a level select screen where you would have a layout group here with your 50 levels here or what have you um if you don't know what a le what a layout group is i'll i'll put a annotation up here to a video on it uh and then each one of those levels would have a the the change level change sense scripts with just a different parameter in it and you would you would just click on it or change the level so if you learned something leave a leave a like subscribe and i'll see you next time for another sexy tutorial have a good day