Unity Asset Bundles — A Basic Discussion
What are asset bundles? Here I want to talk about basic serialized asset bundle files that can be saved somewhere (for example in an AWS S3 bucket) and then loaded for use later at runtime. An asset bundle is basically an archive file that can contain assets such as models, prefabs, scenes, etc.
For more complete information on Asset Bundles check here:
Unity — Manual: AssetBundles (unity3d.com)
One good example of asset bundle usage is for downloadable content. This can effectively reduce the size of an app or program as the content does not need to ship with the product and can be downloaded later as needed.
Here I want to show a quick example of creating an asset bundle for a level in a game. First, we prepare the bundle in the editor by clicking on the asset and giving it a name:
The next thing we need to do is to add the code required to build the asset bundle. Lets start by creating an Editor folder in our Assets folder and adding a script to the folder. Here I will call the script “Create Asset Bundle”:
Next I will add the required code to the new script in Visual Studio. The code needed to do this can be acquired from Unity at:
Unity — Manual: AssetBundle workflow (unity3d.com)
Below is the code:
Basically what is happening is that we are adding a build asset bundles command to our assets menu. We create a directory for our asset bundles (Assets/AssetBundles), if one does not already exist. We then invoke the BuildPipeline.BuildAssetBundles method, which will build our bundles. Refer to the documentation for the the options and build targets available.
Now we can build our asset bundles:
After the asset bundles are built, you should see the new directory and bundles in the project window:
Here I had 2 asset bundles created, one of level2 and one for level3. One file is the manifest (in Unity YAML) and the other is the actual data file. We want to save the data file. These data files can be placed somewhere accessible to our app and be downloaded and incorporated into the app at a later time.
That’s all there is to creating asset bundles in Unity.