Asset Bundles in Unity
- santosh nalla
- Jul 18, 2020
- 3 min read
Asset bundle is a content that is stored separately and is loaded at runtime. For example you have many characters to choose for your game. You do not want to put all characters in your game at the time of release. This would increase the memory of your game. Instead you can store these characters in Asset Bundle and allow user to download (during runtime) . This makes sense right? Why do you wanna put all the characters in your game and increase the size of your game, when you are sure that certain section of user won't even use all characters in their game.
So uses of asset bundles are pretty obvious, right?
First thing, You can have control over your app size. This is because you are giving choice to user, to download content from remote asset bundle.
Secondly - You do not have to push updates. You can add content post release with out pushing update to your application. It like an automatic updates.
In this blog let us discuss first how to implement Local Asset Bundles n your project.
To achieve this, there are 4 simple steps.
Prepare your gameobject prefabs which you want to export as asset bundle. Its like adding scripts, components etc and preparing your working gameobject prefabs. In this project for example i have prepared a "Toon Elephant" prefabs, to which i have added script to move forword once it spawns.
Click on your prefab, and under your prefab you find "Asset Bundle tag". Create a new asset bundle, in my example i have created "elephant" asset bundle. Do the same for the other elephant also. a

3. Ok, now lets actually create an asset bundle and store in some local disk location in your computer. This asset bundle creation is done using simple editor script. This is done prior to running your app.
using UnityEditor;
public class AssetBundleCreator : MonoBehaviour
{
[MenuItem("Assets/CreateAssetBundle")]
static void CreateAssetBundle()
{
BuildPipeline.BuildAssetBundles(@"C:\Users\Santosh\Desktop\AssetBundleFolder", BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
}
}
BuildPipeline.BuildAssetBundle() function takes 3 arguments.
one- string path where you want to create your asset bundle in your local drive. For example i created asset bundle on desktop.
two - assetBundleOption: The optional assetBundleOptions argument modify the way the bundle is built.
In above example i have used "ChunkBasedCompression": This allows realtime decompression when reading data from the AssetBundle. AssetBundles created with this option are stored in compressed form after download (both in disk cache or memory). There are many more which you can find in unity documentation.
three - Third parameter is to choose target platform to build.
This BuildPipeline.BuildAssetBundle() function classifies all the asset bundles in you game and creates a seperate manifest for each asset bundle which have created in the second step. So this is a seperate step and not a bundle specific function.
Once you run this function by clicking on editor tab created(Assets-> AssetBundleCreate), you can see output folder created on your local disk.

4. Now that you have created asset bundle for your prefabs, all your prefabs are stores in local disk space and no longer required in your project. In this way you can eliminate the memory occupied by these prefabs which you have stored under asset bundle.
So delete asset bundle saved prefabs from your project.
5. Now that final question is - How to load the saved asset bundle prefabs from your local location??? (Remember - they are no longer present in your project).
This is the script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadBasicBundleLocal : MonoBehaviour
{
[Header("LOCAL PATH OF ASSET BUNDLE")]
[SerializeField] string _localPath;
[Header("ASSET BUNDLE NAME")]
[SerializeField] string _assetBundleName;
[Header("ASSET NAME TO INSTANTIATE")]
[SerializeField] string _assetName;
AssetBundle _elephantAssetBundle;
// Start is called before the first frame update
void Start()
{
string _finalBundlePath = _localPath + @"\" + _assetBundleName;
_elephantAssetBundle = AssetBundle.LoadFromFile(@_finalBundlePath);
Instantiate(_elephantAssetBundle.LoadAsset(_assetName),new Vector3(0, 0.86f, 0), Quaternion.identity);
}
}
_local path - is the path where you have stored ALL asset bundles(local location/path).
_assetBundlename -So in that path, there might be many asset bundle. So what asset Bundle you are exavtly looking for?
_assetName - Each asset bundle might have many prefabs under them. So what prefab(prefab name) you are looking for?
AssetBundle.LoadFromFile() loads a particular prefab in that particulat asset bundle at that particular path. It's return type is Object, so you can directly Instantiate() that at any particular location.
Final Output:
Comments