Overview
Hi! You’re probably asking yourself “okay, so I know I want this. How do I put this thing together?” If you haven’t already, you’ll need to understand what an asset bundle is. I described it on the Introduction.
Concepts
This tool’s job is to make it possible to load a prefab (or most anything else) from an asset bundle with a single request:
var operation = bundleManager.LoadAsset<GameObject>( assetToLoad.bundle, assetToLoad.asset );
That’s truly all there is to it. This will be an asynchronous call. That means the ‘operation’ is an object that you wait for, for example in a coroutine.
yield return operation;
Because things do still go wrong, error handling is required, but simple:
if (operation.Error != null)
{
Debug.LogErrorFormat("Unable to load {0}, cannot continue. {1}", assetToLoad, operation.Error);
// Tell BundleManager we're done with the bundle.
bundleManager.UnloadBundle(assetToLoad.bundle);
yield break;
}
A common thing to do with a prefab is to instantiate it to make it part of your scene:
var template = operation.GetAsset<GameObject>();
var instance = GameObject.Instantiate(template, this.transform, instantiateInWorldSpace:false);
And when we’re done with the instance, we release the bundle. Balancing these loads with unloads are absolutely essential!
bundleManager.UnloadBundle(assetToLoad.bundle);
That’s all there really is to it! Our loader handles the rest.
Diagram
Here’s the basic design of the system. What follows it is the “Readme” that is included in the project, which outlines specific steps to building and running with your first set of bundles.
Readme.md
This is the entire content of the “Readme.md” file that is included in the project.
Congradulations!
Thank you for installing the Asset Bundle #9’s BundleManagerPro tools!
So now what? :^)
Getting Started
- Open the Scenes folder, SampleScene.
- In the menus, turn on Assets->AssetBundles->Simulation Mode. Expect a checkmark when on.
- Press Play. Expect a capsule to display in the Game view.
SampleScene contents
Be sure to not be in Playmode to match what we describe here.
- In the Hierarchy, select the BundleManager object.
- In the Inspector, look at the BundleManager component. This is the main loader that will keep track of everything. In your projects, this object will probably want to exist for the life of your application. Do not unload it when loading new scenes! You can flag it as Object.DontDestroyOnLoad, or embed it in a scene you never unload.
- Notice all the settings available there. They are all documented on the support website. Largely you don’t need to mess with them, the default settings are pretty good.
- Look at the Inspector for the Prepare Bundle Manager script. This should only need to exist on startup. This object’s job is to load the initial bundle manifest and set it on the BundleManager. The instructions below will describe connecting to a local server. It is this object that will set what URL to load the manifest file from.
- In the Hierarchy, select the LoadAnAsset object.
- This is an example of how to request an asset from the BundleManager. This is expected to happen over and over again during the course of your game. You could put twenty of these in the scene pointing at various assets and they’ll load as soon as they’re ready.
- Notice the ‘Asset to Load’ field looks like it’s linking directly to a prefab in the project. It’s not really! It’s storing only the name of the asset and the bundle that asset is put in. This means that any asset you want to load must be saved on disk, and assigned to a bundle before you can use it in the Asset to Load.
Now this is not the only way to do all this, but this is a way to do it.
So now we’re going to turn off Simulation mode and load some bundles for real.
Building Bundles
To load bundles, we must build the files that will be loaded. This is done the following way:
- Be sure we are in the Standalone platform. In Unity3d, menu File->Build Settings. Select the Platform “PC, Mac & Standalone”. If it’s not already set, press Switch Platform.
- Close the Build Settings.
- Select the menu Assets->Asset Bundles->Build Bundles. Expect it to take some time and show some progress dialogs. With the assets it has now, it should be less than five minutes.
- It will put a message in the console log when it’s done.
- If you like, you can inspect the files by looking at the project folder, in a folder named “BuiltBundles”.
Serving Bundles
We need to offer those bundles through a webserver of some kind. Any HTTP server will do. In this case we will use python which is preinstalled on OSX. In Terminal:
cd <project folder>/BuiltBundles
python -m SimpleHTTPServer
It should show a message similar to Serving from 0.0.0.0 on port 8000
.
You can alter the “Prepare Bundle Manager” script’s “Manifest Base URL” if you need to change the address or port. Remember that it must end with /{0}
where we will add the name of the manifest to load.
Loading Bundles
Back to Unity3d.
- In the menus, turn off Assets->AssetBundles->Simulation Mode. The checkmark should now be gone.
- Press Play. Expect a capsule to display in the Game view.
- Profit.
What Next?
From here, importing the Plugins/BundleManagerPro
into your project is all you need to begin writing your own scripts, referring to this project’s examples.
Additional documentation is available on the Support website.
Enjoy!