How to use Addressables FASTER Loading FREE Memory SMALL Download

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I'm your code monkey and here let's learn how to use Unity addressimals this lets you have more control over how your assets are loaded into memory this is pretty crucial if you want to learn how to make larger scale games you can load an entire world kind of like Skyrim all at once and also pretty helpful to keep your download and install size super small and only download and load the assets when you need them also this video is a lecture taken from my ultimate TNT overview course Unity is massive so in the course I explain over 50 features and tools of the engine that you might not know about there's individual lectures explaining tons of things like Shadow graph similar definitions animation Avatar video player and so on as well as many other Niche things that I couldn't possibly cover in normal YouTube videos also the course will continuously be updated with free updates as I add more lectures explaining more tools and features this specific lecture was added as part of the third free update that explains more tones and features so go ahead and get the phone cores to learn how to master all of the unity tools to help you make better games faster in this lecture we're going to to learn all about Unity adjustments this enables you to have more control over your assets instead of everything being loaded automatically it only loads when you ask it to so it helps you manage memory and also unloading times which can be an extremely important thing especially when making mobile games you want your game to load as quickly as possible and then load some assets over time when they are needed instead of forcing the player to set up the loading screen for a long time also very importantly is addressables is internally synchronous so you're never waiting for your assets to load by looking at a frozen screen it loads in the background and notifies you when it's ready and there's also another part to this system which I'm going to cover in the next two lectures which is how you can store Those ads in a server in the cloud and dynamically push them to your players so they can get updated assets without having to push another build you can download those assets from any server like your own server perhaps your own CDN or Unity solution called cloud content delivery but again that's all in the next two lectures so before that let's just learn how it works locally okay so let's start using addressables this is one of the most requested topics on the comments for this course so let's learn about it thankfully it's actually pretty simple and extremely powerful over here I've got a simple empty scene now I have is a simple massive object this one is pretty much an entire living environment so if I drag it it even takes quite a while over here in DNT editor there you go just a massive environment with tons of objects this prefab is intentionally meant to be really complex with lots of objects so we can then see the memory footprint let's first quickly see the main problem that addressables help solve and then we're going to use it to fix it now with this if I just hit on play and if there it is the game is playing and the object is here as you expect but let's say you don't want to spawn the object right away you want to spawn it on a player input so for that let's make a simple script so let's create a new c-sharp script call it spawn object let's make a new game object to run it attach the script and open so now here let's listen to simple input so let's just do product void update and over here let's get a key down let's say the T key so we have this then over here let's instantiate a prefab so let's add our reference here as a serialized field a private transform for our prefab so we have this and then when we press let's just call instantiate on this prefab all right that's it super simple so this is the basic way to instantiate an object let's see here in the editor just write the prefab and let's hit on play okay the game is running There's No Object now I press the button and if there you go it gets instantiated then spawn okay great so again this is the standard way for how you can instantiate any prefab however when you do it like this if you were to load the scene even without actually spawning the object it would still be included in memory basically anything that is referenced in the scene like for example over here we have reference on this object because it is referenced by this object that is going to be loaded when the scene loads whether it is placed on the scene or not we can make a quick build just to test it out so I just going to file and build settings make sure to include this scene and let's make a simple build alright so here I've got the executable now let's click on it to open it and then count how many seconds the loading is actually going to take so let's double click and wait for the win there we go one two three four fives all right there it is so we took about 20 seconds to open so that's the unloading time again it's because it had to load that entire object into memory even though again right now we're looking at the scene and the scene is completely empty and we can look in the test venture to see the memory footprint over here we can see the executable is indeed taking 680 megabytes now if I press the button to spawn it and there you go it does spawn it and as you can see the memory barely moved so this is the problem ideally you don't want that object to be loaded into memory until it's actually needed doing so will massively speed up the unloading time as well as memory usage while the object isn't being used just think of how impossible it would be to make massive games like Skyrim or GTA if you had the entire world loaded at all times it would be pretty much unplayable so now that we've seen the problem let's learn about addressables and how they fix this first thing that we need is the package so let's open up the window open up the package manager and then over here and let's make sure we are on DNT registry and then scroll down and find the address once package there you go just go ahead and install it okay it's installed now to open up the main window let's go up into window and their Asset Management then addressables and let's open up the groups window okay this is the groups and as you can see we need the button because we pretty much need to create the addressable settings so let's click on this button to create it automatically and there you go that creates a bunch of files at the addressable systems uses and over here we can now see the groups and by default it creates a default local group then manually you can right click to create any groups that you want so you can have as many groups as you want each of these separate groups is going to be built into a different asset bundle but we're going to worry about that in a bit so for now let's stick just with the default group okay then once you've got the groups assigned let me just drag it up here so with the groups assigned we can now add some objects onto it and there are multiple ways to do it and the simplest way is really just to drag and drop the object so let's go over here into my project window I've got my environment so let's click and just drag and just drop it inside the local group and there you go it automatically adds the group so I can see over there the environment prefab being added and over here I've got the path to the environment so with this this object is already marked as addressable alternatively you can select the file in the project files and over here in the inspector you also see toggle for addressable and then you also see the underlying path now over here you can write whatever you want you can modify the path if you want but chances are you should really stick with the default path we're going to see what the path means in a bit also don't worry if you're already scared by the fact that the path is a string we're going to see non-string methods for using addressables another option you have is simply to mark an entire folder as addressable so for example if you want to load tons of assets at once you could make the folder itself addressable and load that for example over here I've got a phone there with a bunch of Sprites inside so instead of going individually and individually marking each of them as dressable I can just drag the folder and drop it over here into my group and there you go I've got an entire folder marked as dressable with all of the objects inside okay so now that we have our environment prefab Marta's addressable let's load it through code so first of all before we do that let's actually get rid of the previous script the previous reference make sure we don't need this so this one is not included in the build okay now let's make an easy sharp script so let's create the new C sharp script call it spawn object addressables let's make a game object to run it and let's attach okay now here first thing we need is our namespace and the main response namespace is inside using Unity engine dot addressable assets then let's make some simple input just like we did a while ago so avoid update if input let's get a key down on the T key so that happens let's load the object using addressables and how we do that we access the actual addressables class and over here let's use the function load asset async again the entirety of addressables is asynchronous the non-async methods like for example here and load sync you can see this one is marked as deprecated so you are not supposed to use this you are only supposed to use the async methods just in case you don't know async means asynchronous which also means that it doesn't plug the main game thread so the game never freezes it keeps playing as an S isn't loaded in the background in a separate thread so let's use this function and this one takes a key this is going to be the path to our asset so here in the address models window let's copy this path so let's select it copy the path here and let's use it here this one is a string again don't worry if you're not a fan of strings we're going to see a method that does not require strings in a bit so that's it just using the string that's the only parameter it takes then also this function as you can see is a generic so that means that we're going to return some type and for this one for a prefab let's make it work as a game object all right so we have our function download an asset and we can see what this returns so this returns an async operation handle let's add the using statement so we've got async operations and this one is going to return that so an async operation handle of type game object okay so we have this and then on this operation handle over here we can subscribe to the completed event so let's listen to this event and when this event happens over here we've got the same async operation handle then on this one first of all we can check if the operation is successful so let's check if this one dot let's check the status so if the status is succeeding if so then we have our object and if not then there was some kind of error so fail to unload this object but if it did succeed which it shouldn't if so then over here we can call instantiate let's spawn our prefab so let's go inside the async operation handle and inside to grab the resulting object that was loaded is inside the result this one as you can see takes a game object which is the type that we're using okay so that's it we go into the address bonds we ask it to unload an asset asynchronously we give it the path for the s that we want to load this returns an async operation handle because again this one is asynchronous so this is not completely loading immediately it does not block the game the code continues to run then grabbing on this one we can listen to the completed event so when that eventually finishes loading in the background when that happens this function is called we check if it succeeded and if so then we finally instantiate the game object okay so with this let's test so here we are and there is no object in our scene now I press a button and this time it takes a bit longer but yep the object is dynamically loaded in the background and when it's ready the object is spawned okay great so just with this simple thing we can already compare with the previous demo so let's make a bill to see that let's do the exact same thing just save the scene go into file build settings and let's make a balance just like the same okay so now it's making a build and this time it actually takes a bit longer to make the balance since it's also going to be compiling and compressing the asset bundle okay so the build is done now once again let's pay attention to the unloading time so let's double click and wait for the window and okay one two and there you go in just two seconds instead of 20. so as you can see much much faster the reason for that is of course because it's not automatically unloading that object at runtime immediately when the build launches and we can verify that by looking in the task manager same here we do see we are currently only using up 137 megabytes so nothing close to the 600 or 800 from before and now if I press the button now the object is being loaded you can see over there it's currently reading and writing to disk so it's loading everything currently into memory and after a bit yep here is our object spawn and the memory did go up so just with this simple demo we can already see the massive difference that it makes our game loaded much much faster and memory usage State and low since the object was only loaded when we actually needed it okay now let's keep learning some more about addressables like I said this is just one method to load you can write a path string and load it also we can simplify this like with any comeback so instead of having to define a separate function and storing our async operation handle let's just call this and then immediately when we call let's subscribe to the completed event instead of using a separate function let's use the unlimited function so let's define an async operation handle and do Al Lambda function and then inside we're going to do the same thing that we're doing here all right so there it is we're still doing exactly the same thing but everything is much more compact this can be useful because it does not require a separate function definition so like I said this method is using a string for the path as a general rule you should always avoid using strings as much as possible strings are super brittle it's really easy to make a mistake with any of it for example if you just miss one letter or just capitalize so the I put it to capitalized if I do this and we test if there we go we have an error because again the string path this one is case sensitive so as you can see strings are very brittle very easy to make mistakes so let's avoid using strings and the best way to do that is using something called an asset reference let's make it a serialized film so we can set it in the editor of type asset reference acid reference let's in the editor what this looks like so there it is we've got our reference and now if we want we can drag it straight from the addressable groups so if I drag the environment drag it on top of there if there you go it does work I can also drag the prefab from the project window or alternatively I can click on the little search button and over here select any of the adjustments included also if you drag a different asset that is not included in the group so right now these are the only ones in group and over here I've got another environment small if I drag this one on there you can see yep it does drag it on there and automatically adds it to the group okay so we have our asset reference now over here we can load this asset reference directly so we just go to this one and inside this one let's call load asset async and again let's do the same thing give it the type so the game object type and we're going to load it and this one takes no parameters and everything else is pretty much exactly the same so let's actually get rid of this and yep everything else is exactly the same so this function still returns an async operation handle everything still works the same except now we don't have strings so it says this is the C so here we are with nothing press the button and if there go it does spawn the object okay so that's the asset reference method another way to load an asset is with a label over here in the addressable groups we can see over here we've got a part for the labels and if we click on each of these drop down minis we can see all of our labels and of course we can add some more so let's click on the little gear icon and over here we've got a list of all of our labels let's create one let's call it environment then we can close this one over here for the environment small let's put it on that one also when it comes to labels you can have multiple objects so if I want this one can also be on there and you can also assign multiple labels so if I click on this yep you can select multiple all right so this object now has in label and for unloading we can use the same method as previously using the key the key can be a path or it can be a label that would work just the same or again if we want to avoid strings we can use a different thing this one is an asset label reference we can use this here in the editor it has a nice drop down menu so let's select the environmental label then here in the code how we want it is by using the previous method so addressables load asset async up type game object and for the key let's pass in just the asset label reference so just this one that's the exact same thing going to the completed and so on so let's see this so here we are and I press the button and if there you go it doesn't load okay so everything is working correctly now with this asset reference you can reference anything so for example over here on the asset reference file you can see that I've got some Sprites that I drag in there so if I go I can select one of these however over here in the code for this asset reference let's go back into the previous method over here we are unloading this asset reference as a game object but here in the editor we drag the Sprite reference if we try running this code and I press a button and there you go got an error we can see the error is because the asset exists as a texture 2D which is not assignable to the requested game object type so this is the error the code is expecting a game object but over here I dragged a different reference because I can drag any reference onto this field there's another example over here I've also got some audio files so I can also drag it on there and there you go that one goes in there and again this one would also cause an error so asset reference can be quite error prone and to some of that there are some helpful reference types that for some limitations on the type of the objects for example we have asset reference game object this one like the name implies only receives game objects then we've got two more so asset reference we've got a textured 2D as you can see we have a Sprite that was right and so on so there are multiple of these to enforce a specific type we can see this in the editor so we have this game object let's put this one there let's see it so here we have the asset reference game object and if I try to drag the coin reference nope I can't do that try to drag the audio clip nope does not work it only works if I drag an actual game object so this would help prevent that error in the code however as you can see there aren't on types so for example there isn't any type for an audio clip but if you need that then you can actually create it yourself if we inspect the code for this we can see that this is pretty much just a class that extends asset reference T and then has a type and just has a Constructor with the go ID so you can pretty much just do this for any type we want so for example let's go up here let's make a public class let's call it asset reference audio clip let's extend acid reference T and then inside prototype so in this case audio clip so you have this and we just need to implement the Constructor so let's use the helpful Visual Studio to create it automatically so here it is we don't need any Behavior anything at all so that's it this works now obviously in order for this to show up in the editor we need to mark this as synonym so system Dot serializable okay and now here we can now use that one so we can use an asset reference audio clip okay let's see this in the editor there's the asset reference audio clip and clicking on the button yep the only thing I can select is indeed an audio clip then for unloading any of the separate types it works exactly the same as previously also one more thing is you can immediately instantiate a reference directly so for example when working with game objects I can be a bit faster than doing all of this so we can go into that asset reference and call instantiate async we can optionally give it a parent essentially in World space give it a position quaternion and so on or if not let's just do this and now this one also returns an async operation so if you want to know when this actually completes you could subscribe to this or just don't do that and just call instantiate a sink so let's comment out of this so we only run this code and if we test this so here we are and press the button and if there you go the object is spawned automatically so if you want this one then this is much more compact than this now like I mentioned you can make an entire folder as addressable so let's see how to load multiple assets at once here in the addressables group I already made my special Sprites folder which inside has a bunch of Sprites this one is now unaddressable then here in the code let's load the entire folder so let's go into addressables and then inside instead of going into load as they sync let's go into load assets async portal let's load a bunch of Sprites then it takes a key so this is the path pretty much like we already saw this one can also take a list of keys so we can give it a list of string then here we can type one path then another path and so on and it will load all of the objects or alternatively we can also load the entire folder at once however this is actually a bit tricky it took me a quite a while to figure this out if you just type in the string for the folder so here in the group if I'm looking there this is the path so let's copy this so over here if I paste that path if you use this method then amp here we can see we get an error alternatively on the acid reference field you can drag the folder and drag it straight in there then over here try to use the asset reference and there however if we try this and now this one also gives the same error so basically we cannot load the folder directly the folders that we see over here inside the address models group this one is really just meant for organization meant for organizing all the objects inside it but you cannot reference the founder directly so the only two options on loading all of these objects would be to either manually drag the reference for each individual object but that kind of defeats the purpose of having a folder with multiple objects so another better approach is to Simply making a label so let's click here and let's go manage our labels let's make a new one special Sprites okay we've got a label so now let's assign it over here and yep as you can see all of the objects inside the folder all of them get the exact same label and now in order to avoid using strings we can go into our object and on the acid level reference let's use that label then over here let's use the ascent level reference like that with this now it will indeed load everything in that folder and then the function over here as you can see has a second parameter this is an action that is going to run on every asset so let's grab the Sprite that is going to run and let's just do unlock on it so debug.log on the Sprite okay so let's test and see if we see unlock for every single object inside that folder so here if I press T and if there go all of the objects so download Gamepad coin which are exactly of the Sprites that I added to this folder also one more thing on folders if you make a Founder addressable and then you drag another object inside that folder so for example if I duplicate an object here so I did not specifically make this object addressable but since it's inside an addressables folder then automatically becomes addressable however there's actually a small bug over here on the addressable groups window as you can see I add that one so I've got dollar and dollar one I've got those two objects but over here I can only find dollar it does not show the other one however again this is just a visual bug if I close the adjustments group window and I open it up again so let's go into window Asset Management Open up the groups and I expand it and yep there you go over here I do see the two dollars so whenever you add objects so for example if I take it out then it's no longer in there but over here it still is so this is just a visual bug anything you put inside this folder will always be included so if I run the build and I'll press the key and over here yep we do see all four Sprites and of course since you can load objects at runtime you can also unload them for that you can use addressables.one lease you can use this one and pass in the async operation handle alternatively you can go into address bones and use release instance this one is useful if you previously used instantiator sync so for example let's test this so let's grab this one so we can't instantiate a sink now we need to keep track of the instantiated object so a private game object for the spawn game object then over here we can't essentially the sync let's listen to the complete event so when this happens we've got over here the spawn game object so let's simply set the spawn to this game object although actually this one does not have a game object that has an async operation and then inside it we get the result and that is what actually contains the game object Okay so we've got the spawn game object reference let's make another input so if input get key down and on this one now let's use the same reference and call release instance and pass in the spawn game object alright so that's it let's just comment this out so let's make a build and look at the memory and see what this does okay so here's the build again since I'm using addressables the member footprint is real low so only 136 now I press the button to download the assets and there you go it starts loading them up and you appear this so the memory went all the way up to 616. now I press the button to unload it and yep there you go the object is destroyed and over there the memory comes back down also another thing is the assets are automatically unloaded from memory when you do a simple scene change so if you were to add Unity engine dot scene management and then up here on a scene if you would do scene manager.load scene if you would load a different scene then yep it would automatically destroy the objects in release the memory although do keep in mind this only works automatically if you use sync on scene mode if you're using additive scene loading then they stay unloaded since Unity does not know which assets you no longer need now there are some more things related to addressables if you want to dive deeper for example as we saw you've got a bunch of profiles so you can click here to go into manage profiles or alternatively go into window Asset Management addressables let's load up the profiles now these are used for easily setting up for example a profile for testing another one for production and so on as you can see each profile defines where exactly is in local and the remote paths I cover how to handle the remote paths in the next lecture then another helpful window is once again go into Asset Management over here we've got these settings then here you have tons and tons of options another one you have is also the useful events so if you see here the Event Viewer this one as you can see has a warning so you need to enable over here on the settings and profiler events so this one needs to be enabled and now if we try running the game like this this one kind of acts like a profiler so if I go and I'll look in the console and I press a button and there you go it does start loading the things and after a while yep it instantiates the object so you can stop at any point and see exactly what it did then over here you also have the analyze window this one lets you pretty much see dependencies it analyzes all of the rules that you define and finds of the dependencies within your asset bundles and finally there's the hosting window this one is for more complex use case where you want to connect to an external host and let you upload builds and content okay so all of that is the basics of addressables a really useful way to have more control over your memory and then Implement load your objects this one helps cut down on loading times in general memory usage you pretty much only load the assets when you need them and of course this lecture is just the basics the next step is to load updated assets from the cloud without having to make a new bone so we're going to learn how to do that in the next lecture and then in the in lecture after that one we're going to learn how to use unity's cloudcon delivery as the CDN for all of our assets so if you're interested in addressables definitely keep watching the next young lectures alright so this was a lecture from my ultimate Indie overview course there's lots more explaining tons of things like Shadow graph similar definitions animation Avatar the video player and so on as well as many Niche things that I couldn't possibly cover in normal YouTube videos go ahead and get the full course and learn how to master all of the unity tools and features to help you make better games faster
Info
Channel: Code Monkey
Views: 86,026
Rating: undefined out of 5
Keywords: code monkey, unity animation, unity tutorial, unity game tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, unity tutorials, how to make a game, unity addressables, unity cloud content delivery, unity memory management, unity fast loading, ultimate unity overview
Id: C6i_JiRoIfk
Channel Id: undefined
Length: 27min 45sec (1665 seconds)
Published: Wed Oct 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.