AssetBundles are a great feature built into Unity3D. They can allow you to update your game with no user interaction or even stream assets after the first install so your package size isn’t too big for mobile. With proper use of assetbundles, you can easily replace textures, gameobjects, and scenes on the fly.
There are a couple ways you can manage your asset bundles, from very basic manual generation to ultra complex systems built for big games with a lot of updates. Today, we’ll cover the new ‘experimental’ asset bundle system. We’ll generate some bundles, and use the bundles to update a game.
Create an empty project… that’s it.
While the new asset bundle system is in development, it can be downloaded from the GitHub site here: https://github.com/Unity-Technologies/AssetBundles-Browser
Download (or clone) the project and copy the “Editor” folder into your existing project.
Wait a few seconds for the editor to re-compile, then open the “AssetBundle Browser” window.
You should see a window like this, with no bundles created yet.
In your scene, create a cube and a sphere. These will represent our player and his enemy.
Give them each a material of their own and color them accordingly.
Here’s what mine looks like.
Create a “Scenes” folder.
Save the scene in the “Scenes” folder and name it “Level1”
Select the Level1 scene in the project view, then look to the bottom of the inspector.
You’ll see an AssetBundle area.
Click on the dropdown and select “New…”
Name the new bundle “level1”
The bundle names must be lowercase. If you put an uppercase name there, it will automatically correct itself.
Save your scene.
Now go back to the AssetBundle Browser window.
You should see your newly created AssetBundle there like this.
The right side is showing the assets included in the bundle. Notice that the Player & BadGuy material are in the bundle as well. This is because they’re used in the “Level1” scene and the system is automatically including our dependencies.
Building the Bundle(s)
Click on the Build tab
Select an output folder for the bundles. This is where bundles will be saved so you can upload them to a web server later.
Check the box for “Copy to StreamingAssets“. Doing this allows us to use the bundle without hitting the web server, and includes it in the build. Once we have the bundle on a web server, we can delete this.
Your asset bundles should be built and if you browse to the folder you selected for the output path, it should have contents similar to this.
Back in the editor, you should also see a “StreamingAssets” folder that contains the same files like this.
Create a new folder named “Code”
Create a script in that folder and name it “Loader”
Replace the contents with this script.
Now that you’ve created a bundle, it’s time to use the bundle.
Create a new scene.
Name it “Loader“.
Create a new gameobject and name it “SceneLoader”
Add the Loader script to the “SceneLoader” gameobject.
Save the scene
Press play and you’ll see your “Level1” scene is loaded.
How does this help?
You may be wondering how this is useful, what have you done that’s special? Right now, we’re just loading a scene that’s exactly what we have in the scene file..
Let’s change that and see if it makes more sense.
Open the “Level1” scene in the editor.
Change the “player” to be green.
Add another bad guy..
Save the scene.
Press play and check out your green player and 2 bad guys…
Stop playing and open the “Loader” scene.
Press play again, and you’ll see that the version of Level1 loaded is what you exported previously, not the current state of the scene.
Real world Scenarios
In a real project, your asset bundle would be the more ‘updated’ version of the level. It could even be a completely new level. You could have swapped out all the art, changed the style, or just fixed a minor bug with the placement of an enemy.
Here, for simplicity of demonstration, we did it a little backward, but hopefully you can see where this can go.
Loading from the web
So you may be wondering how to stream these asset bundles from a web server.
Streaming your assets can be extremely valuable. It allows you to bypass the ‘over the air’ limits for downloading mobile games while not on wi-fi. It also allows you to do your own game updates without requiring any user intervention (no need for players to go download the next version of your game, it’s automatic when they start).
Open the “Loader.cs” script
Replace the contents with this.
Open the Loader” scene.
Now watch as your level is streamed in from a remote web server.
If you look at line 12 of the Loader script, you can see the URL where I’ve placed the bundle.
This call is requesting the bundle (with the required request.Send() right after it).
Then in Update, we check to see if the request is done. If so, we load the asset bundle with the GetContent() method, and load our scene.
Hosting your Bundles
You can host your asset bundles on any web server that gives you direct access to the files. I use Azures Blob container storage for mine, but you can host on AWS, cloudflare, or anywhere else you know how to setup (most of them are pretty easy to get going with).
Simply take the output that you had from the bundle build step, and copy those files into your web server.
Then make sure they’re publicly accessible. How you do that will vary by platform, for Azure it’s a simple menu item in the Storage Explorer.
In this post, I’ve covered the bare minimum basics of creating and loading an asset bundle to do an update. In most real world scenarios, your game and bundles will be quite a bit more complicated, but the concepts still carry through.
There are a few things you’ll want to do differently when it comes to real sized assets on a device, like loading everything async (including scenes). As you get deeper into AssetBundles, you’ll likely build up a mini-ecosystem of your own that handles things cleanly (but again that’s too much to cover in a single post).
It’s also more common to just load art assets and gameobjects instead of scenes. In-fact I’d bet most people never need or want to load a scene from a bundle, but instead just want to update their assets, or load data that configures the scene (can be much less bandwidth, but more work of course).
This is meant as a starting point and hopefully conveys the concepts so you can dig in deeper.
If you’re interested in learning more about asset bundle loading, I highly recommend the following sources.
Unite Q&A on asset bundles (talks quite a bit about the Bundle Viewer asset you used above)
Asset Bundle Experiemental Forum
Asset Bundle Fundamentals – Great info on bundles and how they work, including dependencies, loading and more.