How to download an asset bundle from server in Unity (quick code version)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Kudo and today in this video I will show you how you can download asset bundle from a server so let's get into it so last time in the other video I'll show you how you can create an asset bundle and now let's see how we can download this asset bundle when we put it in the server and how we can download it and instantiate it in in our scene so in this video I will show you the simple way I will make another video on a more complex way to do it with some Advanced code so choose whatever you want but let's get into it so what we need to do we need to create a script so let's create another script so let's create a c-sharp script and let's call it let's call this one so download so download asset bundles simplify simplify way not wall okay and let's open it in Visual Studio okay so what we need to do uh so we don't need the update function I will keep the start function to be able to call my function I just create I will create so let's make uh so a private uh private so I will need a enumerator and let's uh name this function so download so download uh asset bundle from server okay and okay like this so what I need to do I need to add some Library so I will add two I will I will not need this Library I will need to add so using uh so Unity engine oops Unity engine dot networking because we'll able to download some assets from internet okay and basically that's it and in the ear matter what we want to do so we need to create a game object so game object name it game object go and let's initialize it at null and what we want to do so we will need to have a string so this string will be our URL and this will right now let's let him empty because I will put the URL of the asset bundle where to download it and next thing we need to want to do is to be able to download the asset bundle to to retrieve it in unity so I will use using so using parentheses Unity Web request so Unity Web request you need web requests so www equals Unity Unity Web request asset bundle so Unity Web request asset bundle and what we want to do is to get an asset bundle from where so in our URL okay like this and what we want to check so we will wait uh wait until the [Music] the request is sent so we'll say www dot send webrequest and as soon as the send request is finished we will check so if www.result will be equal to Unity so Unity Web requests and it will a connecting error or if www.result equal equal to Unity protocol error okay so we'll check the two case what we want to do we just want to make a debug debug DOT log warning uh let's make it error on the get request so we request add to do that so and let's add our URL and uh let's add what is the error so plus and www dot error like this and we can see uh what our error is so I forget an equal here okay so if this happening will debug this else what we want to do something very easy we just want to create a set bundle so asset bundles will create a bundles will be equal to and will download oops sorry bundles will be equal to download asset Bundle download and download Handler asset bundle queries and asset bundles and what we want is to get content and we want to get the content from our request so the WW okay so now uh now we we add the bundle so what we want to do we want to transform this blender into a game object so what we need to do so game object our game object will be equal to bundle dot load assets load asset this one and it will ask for a string name so this one very easy so bundle dot get all asset name and I want the first one so zero and because uh go is a game object I need to cast it as game object last thing we need to do so we need to make a bundle Dot and load to say uh it's the object is already unload we make it false we can save this and last thing we need to do we just need to make a yelled uh El return and just wait for any of frame so new weight and a frame and guys that's it for the for the unloading of the Bender asset this is very simple code but I will add a last thing as soon as we finish everything what we want to do we can just make a www dot dispose so basically this function just allows us to uh not being used and like this you can kind of optimize some stuff so like this and we'd make a dispose we can save this and now because we just download our Ascent bundles and we our asset bundle is is now into a game object what we want to do we want to instantiate our game object so this one will be very easy what I what I will need to do is will be create a private void so let's call this one instantiate game object from asset bundles I said vendors it will take in parameter a game object game object go and what I want to do something very easy so I will make game object so I will call it instant instance game object will be equal to instance instantiate of our game object and to make sure this will not spawn anywhere so I will say instant scan object dot transform that position will be equal to vector3 .0 so I will call this function inside our enumerator so here what I want to do like this and I want to call this function [Music] with Geo like this and to make sure we don't have any error maybe um maybe the asset is not downloaded right and we try to instantiate what I want to do I want to check if uh game object is different than so different than null okay if game object is different than neuro we execute this else we want to so else what we want to do is just debug.log sorry debug.log um even log warning uh log warning uh your asset bundle game object is null okay so two more thing to do so in the start function we'll start a curvature and we will call our download asset bundle here like this foreign we need to do is to our uh put a string URL so basically this string URL will depend on uh how can I say that different from a user from another so for me because I don't have any server I don't have anything I will use a Google Drive feel free to use mine I will put the link in the description but basically here you should put something my server I don't know my server slash my prefab or my asset bundle asset bundle slash my game object so basically this will be the path uh my game object so basically here it should be the path of where you store your your asset bundles so if you want to try with dry with a Google Drive let me show you how how we how it's done so as you can see here I already create a drag and drop some asset bundle so remember in the last video I'll show you uh where to store the asset bundle so let me show you right now so so here I got the project my YouTube Project and here I got the ascent bundle and remember I just create my first asset bundle prefab so basically what I need to do I just take this one and I will just drag and drop it into my drive like this this will import my first asset Bender prefab second thing you need to do so I will need to get a link and right now what I need to do is just instead of limited I need to uh to put all the user so I will need to copy the link open the link here as you can see here we'll be able to download our asset mender so I will pause the video because as you can see here the the URL is not the same where when you download it and when you view it so I will make sure to make a post to be it will be a you will be able to see what is happening but when I will click on download with Google this will redirect me to another link and this is the link I'm interested of so be careful it will be very quick but don't worry I will make pose and you will see so I think you see what I mean so again if I Again download it here I I was able to copy copy it so now let's head back to Unity and what I'm interested of is to paste our link so let me paste it and as you can see here we got the uh fully um fully URL with the download option so now we can save this and we just need to head back to Unity and you will see our spawn our Cube spawning so make sure to create an empty game object so and add some our uh our script so download Ascent bundle simplify way let's save our scene so maybe we can rename this game object uh download asset bundles I said bundle manager like this we can save this and now if we hit play you will see it will be able to retrieve our Cube and voila so I was able to uh create um available to download an asset bundle from our server so our my drive and I was able to spawn it so this is a simple way to do it in the other video I will show you how we you can handle different type of asset bundle because this this time I use I was able to use a game object but what if I want to open an audio Source maybe I want to open a picture a Sprite or something else I would I will show you different example on the next video so I hope you learned something I hope you enjoyed the video and I see you soon I guess bye bye thanks for watching so if content equal equal to like or user learn something equal to True user should thumb up plus subscribe else users should dislike but help me to improve thank you and I catch you guys next time
Info
Channel: El Profesor Kudo
Views: 10,614
Rating: undefined out of 5
Keywords: Unity, Unity3d, Unity2d, Unity2020, Mobile, codinggame, codingGame, tutorial, HowTo, visualStudio, Android, iOS, indiegame, gamming, videogames, programming, programmation, learning, indiedev, indieGameDev, c#, asset, Bundles, server
Id: sDL8AbEKIsY
Channel Id: undefined
Length: 16min 21sec (981 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.