Make A Game Like Pokemon in Unity | #44 - Additive Scene Loading

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is part 44 of my Pokemon Game tutorial series in unity. In this video, we'll look at a concept called additive scene loading which will allow us to smoothly go from one scene to another without switching them. It loads a scene without unloading currently loaded scenes.

If you're new to it, this a series where we'll create a Turn-Based RPG like Pokemon in unity using Design Patterns and Good Game Development Practices. I'd love hear what you think about it :)

Here is the entire playlist.

πŸ‘οΈŽ︎ 5 πŸ‘€οΈŽ︎ u/GameDevExperiments πŸ“…οΈŽ︎ Apr 19 2021 πŸ—«︎ replies

For Status ailments, I have issues where it does more damage than it should. like my status ailment show max hp/ 8 but it deals more damage than that so I wonder how do I fix that?

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/IceHusky18 πŸ“…οΈŽ︎ May 04 2021 πŸ—«︎ replies

I really like your tutorial series. I wonder what would be a good way to translate your tutorials onto a 3d one?

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/IceHusky18 πŸ“…οΈŽ︎ Apr 29 2021 πŸ—«︎ replies
Captions
hey everyone welcome to part 44 of my pokemon game series in unity so in this video we will look at a concept called additive scene loading this will help us to smoothly move from one scene to another without actually switching the scene so let me explain what i mean so right now i'm in the hometown scene and when i'm in the hometown scene the second scene which is the route 1 scene will automatically be loaded right so if i minimize the game window you can see that when i'm in the hometown scene the root one scene is also loaded and when i go to root one scene all the connected scenes of root one will also be loaded right so here root one has two connected things one is home town and one is down two so by using this approach we can go between scenes without actually switching them and we'll also unload the scenes that are not connected to the currency so for example if i come back to hometown you can see that the town 2 scene is unloaded since it's not directly connected to hometown so let's look at how to implement this special thanks to all my patreons for making the series possible by becoming a patreon you can support me in the making of the series and get access to some cool rewards like the complete project files of the series which also contains some advanced features that are not covered here so let's start the video so i have cleaned up the hometown and root one scene a little so this is the home brown scene and this is going to be a root one scene all right so now to implement additive scene loading i'm going to create a new scene called gameplay all right so this will be the scene to run in order to play the game and from the scene we will load all our levels so let me just delete the main camera so this scene is empty now so first let's look at how to open multiple scenes at the same time in unity so right now the game placing is open and if i right click on the home town scene and select open scene editive then it will also be opened alongside the gameplay scene all right so let's also open our root one scene so now these three scenes are open at the same time so what we have to do is we have to arrange all our scenes next to each other and then load them dynamically from the gameplay scene so right now you can see that the root one scene is on top of the hometown scene so we have to change their position and connect them in order to create our map okay so i'll select the grid from root one and i'll move it above the hometown scene all right so i'll change the y to 20 in order to align it perfectly so now the root one scene is right on top of the hometown scene and when using this method we don't need portals to switch between scenes so let me delete the portal from root one and the home hometown all right and by the way don't delete the portal of the indoor scene we'll still need portals to switch to the indoor scenes all right so now if we test the game we have both scenes connected like this and we can easily go from one scene to another all right but this is just temporary one thing to notice that the hometown and root one scene is not inside our gameplay scene it's just opened alongside the gameplay scene so if i change to some other scene and then come back to the gameplay scene you can see that it's empty so what we have to do is we have to dynamically load the hometown and root one scene into our gameplay scene all right so to do that i'll create triggers for each scene inside the gameplay scene and these triggers will be responsible for loading the scene so once again i'll open the hometown and wrote one scene and now inside the gameplay scene i'll create a new game object called hometown all right and i'll reset its position so this is going to be the trigger that will be responsible for loading the hometown scene okay so i'll add a box collider 2d to it so the box collider is pretty small so we have to make it as big to cover the entire hometown scene so let me increase its size since i know my hometown scene is 20 by 20 i can manually enter that and then i can change the offsets so that it covers the scene okay so now the trigger covers the scene perfectly so next we have to check the is trigger checkbox so this will make it a trigger instead of a collider all right so next we need to be able to detect when the player enters this trigger so let's add a script to do that so inside script inside scene management i'll create a new script called scene details and let me drag the script onto our trigger so inside the script i'll use the on trigger enter 2d function so this function will be called whenever some object enters the trigger of this game object so inside this function we have to make sure that the object entered was the player so for that i'll check if collision dot tag equal to player and if that's the case then i'll log a message saying enter and the name of scene okay so we can get the name of the scene from gameobject dot name so yeah i forgot to mention it whenever you are creating a trigger for the scene you have to give it the same name of the scene so this is because we'll be using the name of the scene in order to load it all right so when we get game object dot name it should print hometown right so in order for this to work our player game object should have the tag player so if we open the player let's actually turn this into a prefab so that we can easily access it and let's open the player prefab so here you can see that it's untapped right now so for our code to work we need to tag it as the player all right so another thing that's required is the player must have a rigid body and a box glider 2d alright so we added this in the first video when we created the player so in case you haven't done that then make sure to add these two all right so let's test if our trigger is working so if i play the game the message entered home down is being printed over here all right so the trigger is working so next let's create a trigger for the root one scene so first let me turn this into a prefab all right and i'll name this prefab something like scene trigger so next i'll duplicate this and create a trigger for root one see so right now the root one trigger is on top of the hometown scene itself so let's move it up and and let's make it cover the root one scene okay so i'll set the y to 20 in order to align it perfectly so yeah now we have sugars for hometown scene and root one scene so if we test the game by default it will show enter hometown and when we go to the root one scene you can see that it shows under root one all right so if i come back it will show android hometown and when i go back to root one it changes so in case this is not changing for you you might be using the collapse button so if the collapse is on it will be stacked on top of each other and it will not change while testing all right so next we have to dynamically load the scene when the player enters its trigger right now we are not loading anything so if we go to some other scene and come back to gameplay scene and if we try to test it you can see that none of the scenes are loaded so we should dynamically load the scene from these triggers all right so in the scene detail script if the player enters the trigger i want to load the scene with this name right so how can we load a scene we can use a scene manager dot load scene function just like we did in our portal script okay so here i'll call scene manager so in order to use scene manager i have to import unityengine.com and i'll call load scene async function so here instead of giving the scene index i'll pass the name of the scene all right and when loading the scene like this we want to load the scene editably right when we load the hometown scene the gameplay scene should not be destroyed so in order to load the scene additively what we can do is we can pass the load scene mode as the second parameter so for the second parameter i'll pass load scene mode dot additive all right so this will load our scene additively without destroying the scenes that are currently open so next if the scene is already loaded then we have to make sure not to load the scene twice so for that what i'll do is i'll create a boolean card is loaded so this will keep track of if the scene is loaded or not and we only want to load the scene if the scene is not already loaded okay and after loading the scene i'll set is loaded to true so this will make sure our scene will only be loaded once so let me place this code inside a function since we have to call this from multiple places so i'll create a function call load scene and i'll paste code to load the scene all right and from here i'll just call the load scene function so now the scene will be loaded when the player enters its trigger so let's go back to unity and before we test we need to add the essential objects loader into our gameplay scene so let's do that so this will load the player and all the other essential objects for us so let's test the game all right so you can see that the hometown scene has been loaded dynamically and if we go to the root one trigger then you can see that the root one scene has also been loaded so if you pass the game you can see that all these scenes are being loaded additively so that works so next when loading a scene we should also load all its connected scenes so let me explain what i mean so the hometown and the root point scene are connected right so if i go through here i can go to the root one scene so what we have to do is when we load the hometown scene we should also load the root one scene all right otherwise the problem is we'll see this dark area over here instead of the root one scene all right so we need a way to indicate that two scenes are connected and when loading a scene we should also load all its connected scenes all right so for that inside the scene details script i'll create a new list of scene details and call this connected scenes okay so now when the player is on this scene we also have to load all its connected scenes right so after loading the current scene we need to load all the connected scenes so i'll use a for each loop to loop through all the connected scenes and i'll call scene dot load scene in order to load each one of them okay so this should load all the connected scenes so let's go back to unity and assign the connected scenes so you know that hometown scene is connected to the root one scene and the root one scene is connected to the hometown scene so to test this properly we need to add one more scene all right so first let's open up our hometown and root one scene and then i'll duplicate my hometown scene to create another scene car down to all right so down two will be after the root one in the map so let me also open down two and right now it's just on top of the hometown so i'll select the grid and i'll place it next to root one okay so let me change the x to -20 and i'll change the white to 20 all right so we'll be entering the town two from root one so when duplicating and moving a scene like that make sure to move all the other elements in that scene right so the npc of the town two scene is actually right here so we need to move that to down two and the portal for this house also need to be moved but for now i don't need a portal for the house so i'll just delete it and that also delete the trainer from here so yeah that's one thing that you have to be careful about when duplicating a scene make sure to remove or move all the objects in it so next i'll just open a way in this fence to enter the town too so let me do that really quick and you can remove this layer of fence inside down to since we already have a fence here so let me do that all right so finally let me just fill in the gaps so next i'll make some changes in down to just to differentiate it from the home town all right so i'll just place something like this over there alright so we have created our down to scene so next let's add a trigger for it so i'll just duplicate the trigger for root one cleaning it to down two and i just have to move it left and make it cover the down two so here i can just use -20 to align perfectly so now let's look at how the scenes are connected so the home town is connected to route one and the route one is connected to both the home town and down to right from route one we can directly go to the hometown and we can also go to town to so it's connected to both hometown and town two and the town two is only connected to the root one okay so that's how we should define our connections so if you look at the home town it's connected to root one dot one is connected to home town but it should also be connected to town two so let me drag down to here and finally down to only needs to be connected to root one okay so let's close every other scene and just open the gameplay scene so before we test the game we have to add the town to to build settings so let me open the build settings and drag down to over here all right so now let's test if the connected scenes are loading properly okay so i'll minimize my game tab and i'll place it over here so i can see what's happening in the scene window so yeah initially you can see that only the home town and route 1 is loaded so as soon as the player reaches the root one scene you can see that the town two is loaded over here all right so all the connected scenes are loading properly so next we have to make sure to unload all the unwanted scenes so right now if i go back to hometown you can see that down two is still loaded right but we don't want that if a scene is no longer connected to the current scene then we should unload that scene so let's look at how to do that so in the scene details script after loading all the connected scenes we can unload the scenes that are no longer connected so for this we need to get the list of previously loaded scenes right so what i'll do is inside the game controller script i'll keep track of the current scene that's loaded and the previously loaded scene all right so i'll create a property over here all right this one's going to be the current scene and i'll create another one to store the previous scene and then i'll create a public function to set the current scene so in this function before setting the current scene first i need to store the current scene into the previous scene right and then i can change the current theme all right so we need to call this function whenever we load a new scene so here i'll call gamecontroller.instance.set currency and pass this as currency all right so now we have a reference of the current scene and the previous scene so to unload the scenes that are not used first i'll check if the previous scene is not null so i'll say game control dot instance dot previous scene it's not normal so if that's the case it means all the connected scenes of this previous scenes were loaded earlier right so what we can do is we have to loop through all those scenes and we can unload them if they are no longer connected so i'll create a variable called previously loaded scenes so this will be all the connected scenes of the previous theme okay so next i'll loop through all the previously loaded scenes and if they are no longer connected then we can just unload them so for that i'll say if connected scene does not contain the scene and also this scene should not be equal to the currently loaded scene right so what we are saying is if the scene is not the currently loaded scene and if it's not in the connected scene of the currently loaded scene then we no longer need it right so we can just unload that scene so first let me create a function to unload a scene so i'll just copy this load theme function and rename it to unload so we should only try to unload a scene if it's already loaded and in order to unload a scene we can call unload scene async and for the unload we don't need to pass the scene mode so let's remove that so this should unload the scene and after unloading it we can set is loaded false all right so now let's call this function from here okay so this should unload the scenes if they are no longer connected so let's try this out okay let me minimize the game so yeah right now we have hometown and root one loaded so if i go to root one now you can see that down two is loaded and now if i come back to my hometown as you can see that down two is unloaded so the unloading is working fine so let's try going to town two okay so when we are in town to the hometown is unloaded so yeah our logic to unload the unused scenes are working fine so i'll stop the video here in the next video we will look at how to use this same approach for the indoor scenes and we'll also fix some of the bugs that we currently have so right now you might have some bugs in file pokemon encounters so we'll fix things like that in the next video so before you leave please make sure to leave a like on the video and consider subscribing to my channel that'll really help me out so i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 2,219
Rating: undefined out of 5
Keywords: make a game like pokemon in unity, how to make a game like pokemon in unity, make pokemon in unity, make pokemon game in unity, how to make pokemon game in unity, make a pokemon game in unity, unity pokemon game tutorial, unity additive scene loading, unity scene transition, unity scene switching, unity scene manager
Id: eZR0icj3U4w
Channel Id: undefined
Length: 28min 29sec (1709 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.