What are Subscenes in Unity? (Massive Worlds!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to look at sub scenes in unity this is an excellent feature that enables you to build massive worlds with excellent performance by utilizing unity dots and dynamically loading and unloading chunks of your world let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you learn how to make your own games with NF tutorials made by a professional indie game developer so if you find the video helpful consider subscribing okay so here we're going to look at what are sub scenes and how we can use them we're going to create a system to load and unload sub scenes on-the-fly this will enable us to have massive worlds who are keeping excellent performance an example of this system in use is in unities mega city demo project in that project the city is absolutely massive with each building having over a hundred thousand game objects now if you loaded the whole city at once it would not be playable at all so instead what happens is the assets get streamed as they are needed now that system is a bit more advanced and what we're going to do here but still a very similar approach the main difference is instead of just loading and unloading sub scenes it also has scenes and sub scenes with different levels of details essentially it's a lot system like what's commonly used in meshes but for entire sub scenes so every single building has two representations a level level of detail scene and a high detail seen the alone level is very performant with low detail and the high level gets loaded as the player gets close so that means our world in total can have millions of objects because only the ones that are needed are loaded at the same time ok so this is what we want to make over here is my player character and I can simply move them around now as you can see there's a map with some rocks and objects and stuff and I can move around and as I move you can see that the map is still there it goes in out and there you go I can keep moving and the map is always there however it's really not always there the map is actually being loaded and unloaded based on the player position so if I pause right here and look in the editor there go over here you can actually see what is happening our map is split into a whole bunch of sub scenes and only the ones near the player are currently loaded so over here as the player is moving you can see the various parts of our level being streamed in and out of existence so with this way you can have some really massive worlds while keeping your game CPU time and memory consumption very low so the game only ever loads exactly what it needs and nothing more so using this time setup you can essentially make infinite worlds without any issues with performance and this is not just in the game one of these prongs benefits also work in the editor so when a sub scene is unloaded there you go there are no game objects or entities occupying any space-time this video is made possible thanks to these awesome supporters go to patreon.com/scishow code monkey to get some perks and help keep the videos free for everyone alright so this is our very interesting system that is made possible thanks to sub scenes so let's look at how they work okay so here we are in our starring scene I have my player character and not much else just a bunch of objects and a background and I can move them around so here we are in the editor and you can see the player right here it is a simple quadratic do identify the player and then being converted into an entity and then we have the background on the sprites rocks bushes and so on so it's placed a bunch more objects okay here we have a bunch of random objects now in order to make a sub scene it's actually extremely simple all we do is we select all of our objects so let's select all of these okay now we right-click and create new sub scene from selection and yep just like that the sub scene has been created so now all these objects are inside the sub scene so over here we can click on the arrow to expand it and there you go we can see all of the individual objects inside of our sub-theme now if you some like the sub scene and look in the inspector you can see some buttons to interact with the sub scene so right now as you can see in the hierarchy we can view all of the individual game objects inside of our sub scene so that means that our sub scene is currently open so we are looking at the game object representation this is useful if we want to modify things so let's say this rock move it a bit over there okay now when I'm done setting up all the objects I can some like this obscene make sure save any changes and then click on close and once you do you can see that on the hierarchy the sub scene has been closed so we no longer have an arrow and we can no longer edit individual game objects instead what is happening is all of our objects are being represented as entities so if we want to edit we click on edit then we have all of our game objects again so we can move them around then when we're done we make sure that we save and we close and there you go we have super fast entities now the conversion is stored in a binary file now that's important because that means that loading is super fast since all of the conversion is done right now and not when we're actually loading so this makes loading and unloading during runtime extremely fast if we run the game right now yep there you go everything still looks the same so I still have my player I can move around and I can see all the objects and if we pause there ago we have our sub scene here in the hierarchy and if we look into the entity debugger over here we can see all of our entities and then here specifically we have two special entities so on for our scene and one for our scene section in the scene section you can see the streaming state that is currently unloaded and if we select our sub scene and look in the inspector over here we have button to unloaded so let's look at the game at the same time now click on unload and there we go all our objects that are now gone so the sub scene has been completely unloaded if we look into the entity debugger any up over here you can see that we just have the scene and the scene section and all the other entities are now gone so you can see how sub scenes are a great way of organizing objects and easily unloading or unloading them also inside of a sub scene you don't need to add the convert to entity script so for example if we open this one and then go into this object and I add the convert to entity script there you go I have a nice notice in here essentially it's saying that this object is already going to be converted inside of a sub scene so this component is meaningless in here every single object inside of a sub scene already gets converted into an entity the second that you hit close all right so here you can see the basics for how sub scenes work you essentially take a bunch of game objects and put them inside of a sub scene now when the sub scene is closed or the game is running then everything inside it is using super fast entities and when you click on edit and open the sub scene you can go back into the normal game object representation and edit the objects as usual and when you're running the game if the sub scene is unloaded then of the entity cease to exist which means they don't take up any memory or CPU time and again sub scenes are stored in a binary file so I'm loading and unloading is insanely fast ok now let's look at how we can dynamically load and unload sub scenes okay so let's make the system to load and unload let's make a new c-sharp script call this our sub scene loader let's make this a simple component system okay now here on our update let's test for a key down when we press the sweeper arm let's load our sub scene so let's make that function okay so we're going to have our function that receives an object of type sub scene and now in order to load the scene we need to load it using the entity component system we need to get the system of type scene system so let's grab it on our oncreate so we go into the world in order to get or create the system of type scene system and then we can use our scene system in order to call load scene async and here we need to pass in the scene ID so we go into the sub scene in order to get it all right so that's it that's how you load a sub scene now all we need this to know our actual sub scene object so here let's make a script to hone those references okay there's a script on this game object and here let's just add a film and make it a singleton all right so we have our scene reference and back in the editor here we have our script to the film for our sub scene and we just drag our reference okay and now we can go back into the sub scene loader and when we press the space key let's call our loads obscene function and person from our sub scene references all the instance and get the map sub C alright so we should be able to load our sub scene whenever you press the spacebar and just a test over here and let's select our sub scene and let's disable out a load scene so by default it will not be unloaded okay let's test so here we are running the game and yep there's no background no world and if we pause and look at the debugger over here yep we just have our scene and scene section so no entities and now if I press space there you go we have our sub scene being loaded so you can fast look at it and yep there you go we have our sub scene so we need is this simple function call in order to load a sub C now in order to unload it's pretty much the same thing we go into our scene system and we call unload of our scene so on space let's load and on another key and let's unload alright let's see okay here we are in this obscene start off unloaded now press space and there go the sub scene has been loaded now press the other key and there you go now it's unloaded a load it unload just like that and you can see that it's exactly instant awesome okay now let's make it load and unload based on the player position so here are the sub scene let's do an entities for each on D player all right so here we have some very simple code so we grab the player translation then we do a simple math dot distance between the player position and the position of the transform for the sub scene and if it's under a certain distance and we load if not then we unload all right so that's very simple everything should be working let's test okay here we are and the scene starts off loaded and as I move away after a while as I get far enough there you go the scene unloads and I move back to the left side and when I'm near it loads go away and unloads so there you go just like this here I'm looking at the scene in game side by side you can see I'm boy unloaded move back and now it's load all right so here you can already see the basics in action now let's just build more of them out all right so here I built a lot more of the map so you can see I just duplicated made a ton of sections and all of them have been converted into individual sub scenes now in order to load and unload based on distance we're going to use the actual position of the sub scene object now this is probably not the best way to do it but just for testing it should work but for example let's say this sub scene there you go it's in there so let's move this obscene transform to put it right there and there you go you can see this little info message telling you that offsetting the sub scene doesn't actually offset the object inside which is fine since we don't really want to offset them we're just using this position a more proper way to do it would be to add some sort of custom inspector and you would simply have a vector3 with a nice little widget but in this case just for testing this should do it so let me just locate all of the transforms alright so i've located all of them now let's go back into our sub scene references script and here instead of just a single sub scene reference let's make an array and back in the editor we can select our sub scene reference script yep there you go there's our array now we can lock the inspectors and like all of our sub scenes and drag them all up to your right yep there you go there's our sub scenes and in our loader script let's do it based on distance for all of our sub scenes alright so just like that we cycle through our sub scene array and we test for distance and if comes enough we load if not then we unload all right so that's pretty much it and let's test ok here we are and it seems perfectly fine there's my player and over here we can see our map and if we pause and look at the editor if there you go only this one is loaded so all the other sub scenes are currently unloaded and here in the entity debugger yep you can see all of our sub scenes all of our scene sections and pretty much only one of them is loaded so now here if I move the and I go towards the right and there you go over here I'm going inside another map and yep it's downloaded so if we pause yeah but where you can see that now we have two sub scenes being allotted so here on the side by side you can see that as I move the player as it reaches close enough there you're one of them gets unloaded and one of them starts being loaded and as I go if there you go just like that all of them are working so they are being streamed in and out based on the player position so our game scene is super performance since only a tiny bit of the map is actually unloaded at once all right so here you can see how the sub scene system works and how you can stream parts of your level in and out again this system is integral to how the megacity demo was made without it it would simply not have been possible so sub scenes allow you to build massive worlds from keeping your game and your editor performant since you can easily load and unload sub scenes on the fly and with sub scenes you can also edit them in order to modify them using the normal game object representation so you added game objects then it gets converted into super fast entities so streaming sub scenes in and out coupled with the insane speed of unity dots means there's pretty much no limit to how big a worm you can create so go ahead and get started working with sub scenes and make some massive worlds this video is made possible thanks to these awesome supporters go to patreon.com/scishow code monkey to get some perks and help keep the videos free for everyone as always you can download the project files in a tony's from unity code Montcalm subscribe the channel for more unity tutorials post any question have any comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 89,709
Rating: undefined out of 5
Keywords: unity subscene, unity subscene tutorial, unity subscene ecs, unity dots, unity ecs, unity massive world, unity world generation, unity infinite world, unity entities, unity entity component system, code monkey, brackeys, unity tutorial, unity game tutorial, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming
Id: 91kJtsDLTyE
Channel Id: undefined
Length: 15min 51sec (951 seconds)
Published: Fri Feb 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.