Scene Manager in Unity (Loading Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I'm your code monkey in this video we're going to create a class to handle scene loading let's begin so this is what we're trying to create we start off with a very simple basic main menu we have a play button when we click it loads the unloading scene displays loading in a progress bar and when it is finally unloaded it loads our target scene and here we got a button to go back and so on and so forth so the loading scene works perfectly fun all right so let's get to it let's start off by making a loader class so we go in here we create the new c-sharp script and call this the loader this will be the class that handles all of the leveling so in here let's get rid of motor behavior and make this a static class that means we can only have static methods and fields we cannot instantiate this class which for a loaded class is exactly what we want so let's start off with a very simple log function so we make a public static void low and inside we're going to use the function scene manager dot load scene this function is inside the using unity engine scene management all right so this is the function that allows us to load a particular scene so in here we need to know the name of the scene we want to look so instead of using strings all over our code let's use an enemy instead so in here make a phony kanoon for our scenes now let's see the name of our scene so here it is a new project files you can see that this scene is called the game scene so let's add that valley now on the load function will receive a scene value can we use that scene value to load it and we simply do it a two string that way in our code is dealing with a very nice genome instead of just strings all over the place so now we have this function that is very simple and we can call so here my testing scene I have a button in the UI so let's use knot it contains this script so in here we can go into this function to go into the unloader class and call the unload function and we pass in the scene for the games so essentially clicking on the button should reload this quarantine will later obviously make this go into the main menu and then have an our button to go to the game scene from the main menu but for now just for testing let's simply reload the currency now this button is using the button UI class which is part of the code monkey utilities which as always you can grab for free from me in Tacoma Keep Calm it's just a very simple button class that handles buttons in the yuan so when we click on the button we execute this function which executes this code which loads the current scene all right so let's see if our scene doesn't need reload wouldn't click on the button okay so here we are this is the game scene just as a rotating sprite and if I click on the button it should reload this scene so I click on it and yep everything is frozen the scene is loading and the scene has correctly reloaded so loading is working correctly however one loading one issue that you can see is that the game hangs on the current screen while loading we want to have a proper loading screen being displayed on the scene is loading otherwise the player might think that the game has crashed so let's do that we're going to create a new scene this will be our loading scene now in here let's just set up our UI so for that let's just simply create a new text object so just like that it automatically created a canvas with a event system so here on let's just set up this text field to say loading and put it on the corner so this is what the welding scene looks like we just have a black background just saying loading so now in order to use this let's go into our loader class and in here let's add another scene value for the unloading scene and before we load our target scene let's first load the loading scene so we will in here these scene top loading and now in order to test this we need to go in here go into file build settings and we need to add our scenes in here otherwise the unloaded code will not work so let's drag the game scene the unloading and the main menu which we want later use all right so let's go back into the game scene in order to test out our button so here I am again the scene click the button and as you can see it's loading and it reload the same scene but it did not show the unloading scene so let's go into the code to see why that happened so here we are in the code and the reason we didn't see the unloading screen is right here in these two lines these two lines follow one another so as soon as the unloading scene finishes loading it immediately starts loading the target scene now the screen only refreshes on update so if there's no update there's no refresh so what we need to do is let it run for at least one update in order to refresh the screen and then we can load our target scene so for that let's create a script that won't call a function on the first update so let's go here create a new C sharp script and let's call this the unloaded come back in here let's have a simple private ball called is first update and defaulted to true and then we simply do a private void update and if this is the first update then we set it to false and we need to call a function on the unloader class alright so just like that we have this script which will call this function exactly after the first update so let's create that function go into the loader and here make a public static void and loader come back alright so we have this function being called after the screen refreshes now we can use this to load our target scene so in order to do that let's use a delegate to store the action we're going to execute so let's go here make it private action for the on loader comeback a action is just say don't forget that returns void so when we get the unloader combat function being triggered we test if this one is not known we can't let them and we set it to not okay so now all we need to do is put the target scene loading code inside our loader combat action and just like that now obviously we want to load only after we set that so do that and that all right so that's pretty much it when we call the unload function we give at the target scene that we want to load we set the action to load that target scene and then we start loading the unloading scene when the loading scene gets finished loading it won't fire this function and this function will execute the unloader come back which won't start loading the target scene so that way we have a refresh of our screen between loading the unloading and loading deterrent now all that remains is to have the unloader convex script into an object so here and let's go into the unloading scene here creating new game object for the unloader come back and we simply drag our script and just like that everything should be working and we should now be able to see the loading scene while the target scene is loading alright so here we are in the game scene now let's see what happens when I click on the button and if there you go there's no loading scene now it's loading and when you finishes loading it refreshes with our target scene all right great so everything is working exactly as intended now let's make it load between the main menu and the game scene instead of just reloading this same scene so here all we need is on the game scene UI instead of loading the game scene this one goes back into the main menu so need to go onto the scene list to add the main menu scene and on the main menu we have pretty much the same code and we do loader load and in this case we unload the game scene alright so now let's start off at the main menu and see okay so here we are in the main menu which is a simple play button I click on it there's no loading screen and yep there's the final scene being correctly loaded click on the main menu there's a loading and immediately jumps into the main menu alright everything is working exactly as intended so if all you want is a very simple loading screen then this code will do perfectly fine now we can try improving our loading by creating the unloading bar however whether that bloating bar works or not will depend very much on how your target scene is set up if you have a your target scene with lots of prefabs and lots of models with many different textures then unity might be able to break it into pieces and show the loading bar correctly however in this case the code I have for testing the unloading as you can see it stays in here for about two seconds and loads the code that I have in here this nationally get broken up by unity so by implementing the unloading part we won't actually see it working but still let's go over the code to see how it's done so let's go into the unloader class and here instead of having the unload scene function we can use the unload scene async function this lets us load the target scene asynchronously which means the game supposedly stays active while it loads in the background now this function returns a async operation object which will contain the unloading state and according to the unity documentation the right way to use it is with a KO routine so let's create that now in order to have a KO routine we must have a function that returns a I enumerator so let's make that and inside this function is when we're going to load the target team which returns a async operation object so up here we're going to use load scene async instead of the scene manager okay just like that however we want to start this as a core routine and again in order to start a KO routine we need an instance of a script that extends motor behavior and in this case our motor class is static and doesn't use mana behaviors so we cannot just start the routine using this class so we need some sort of dummy object with a dummy component so let's do that simply go in here make a private class let's call this the unloading mono behavior and we extend mono behavior and we sim only become wholly empty again this is just a dummy class so now here in our load function we can create a loading game object then we add the component for our loading model behavior and now we can use that loading model behavior to start a coup routine and we're going to start it using these parameters so this is really one of the reasons why I don't like using core routine since it requires such a complicated pattern in order to make them work but in this case and let's follow what the documentation says and use it exactly as it is so right now we have this function being executed as a cool routine which means it runs over several frames so for example we can put a yield return no thread on the beginning and that way this will go past one frame before starting the loading then we start loading and we get the async operation object and now in here we need to do a while so we do a while async operation that is done so we do a while it is not done while it is not done we're simply going to wait for the next frame so we do a yield return known and when it is done it will exit this and a lot of the targets seen and that's pretty much it now again this code might seem confusing but that's really just because of how Co routines work and in reality it's section very simple so now that we have this code working and loading our scene asynchronously we need to also have a function that will return the progress of that scene so we can later apply it to our bar so we can make a function here a public static float which will return the unloading progress and in here we're going to need information from the async operation so let's store it as a variable and here if it is not no then we return the async operation dot progress and if it is no we simply return Juana all right so we have this function which will return a value between 0 and 1 containing the progress of our loading so now all that remains is setting up the unloading bar in the editor so let's go in here into our loading scene and let's quickly create a very simple bar so here we have a very simple bar it is just an image inside I'm using the texture which is a simply white 1x1 texture I'm only adding that so I can access the image type so we can use the film the image type fill it from horizontal from the left side and we have in here this nice value the film amount which we can set between 0 and 1 in order to easily display our loading progress so now let's write a very simple script to hand on this bar so we create a new C sharp script this will be the unloading progress bar and then here all we need to change is the film on so let's first grab a reference to our image and on the private void update we simply set the image without film amount and we ask the another class to get the unloading progress and that's it that should do it so let's test so here's the main menu and now when I click yep there's the unloading screen and the unloading bar which was 90% filmed and now here we are on the final game scene and everything worked now again we go back to the main menu and worked go back and down and it is working exactly as intended now however as I said I'm not sure how unity breaks a scene apart piece by piece but it only seems to work on some cases so in this case you see the bar go from empty directly to 90% before loading the final scene the code is all correctly working but the smoothness of the loading bar will depend on how your target scene is set up so there you have it we created the unloading scene with the unloading bar for our game it displays the unloading progress won't being very easy to use adding more scenes is just a matter of adding another valley to the unknown and everything works perfectly fine as long as you can download the project files and utilities from Unity code monkey comm if you have any questions post them in the comments and I'll do my best Internet subscribe for more videos and I'll see you next time
Info
Channel: Code Monkey
Views: 175,015
Rating: undefined out of 5
Keywords: unity scene manager, unity scene, unity scene change, unity scene tutorial, unity scene manager tutorial, unity scene manager script, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity 3d tutorials, unity tutorial 2d, unity game, unity 2018, beginner, easy, unity2d, unity3d, unity, basics, basic, game dev, code monkey, how to, learn, tutorial, course, game development, to, how, series, tutorials, unity 2023
Id: 3I5d2rUJ0pE
Channel Id: undefined
Length: 15min 20sec (920 seconds)
Published: Fri Apr 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.