Using Asset Bundles to load external asset in Unity

Valéry Raulet
4 min readJun 3, 2023
Photo by Sean Stratton on Unsplash

When developing with Unity, you may want to load assets only when needed. Having them in your main project may lead to a very large installer. To solve this issue, you can create assets externally to your project and store them in an AssetBundle that will be loaded whenever required.

Note that Addressables is a more modern solution that can replace AssetBundles.

Creating an AssetBundle

To create an AssetBundle, you must first identify what files you want to add in it. To do so, select an Asset in your Project and go to the Inspector. At the bottom of the window, you will notice an AssetBundle drop-down:

AssetBundle Setting in Inspector

Press New… to create a new AssetBundle group and give it a name.

Let’s stop here and let me give you a few rules:

  • An AssetBundle name is always lowercase;
  • An AssetBundle can contain one or multiple Scenes. When doing so, you can add other type of assets in the same bundle;
  • An AssetBundle can contain any asset types;
  • AssetBundles are generated for a specific platform. If you want to support multiple platforms, you then need to generate the AssetBundle for each platform;
  • Custom scripts are not included in an AssetBundle. More on that later!

Once you have specified what you want in your AssetBundle, you need to create it. You can use a script or use the AssetBundle Browser

AssetBundle Browser

Installing the AssetBundle Browser

The AssetBundle Browser was developed by Unity but they stopped supporting it. Despite this, it is still functional and a handy tool!

To install it:

  1. Navigate to Window > Package Manager
  2. Select the + button and choose Add package from git URL…
Add package from git URL…

3. Type the following URL:

https://github.com/Unity-Technologies/AssetBundles-Browser.git

4. It will install the tool called Settings Manager. Note that the latest version is 2.0.1 (6 December 2021):

Settings Manager

5. Once installed, you can access it from Window > AssetBundle Browser.

Creating a bundle with the AssetBundle Browser

When you open the AssetBundle Browser, in the Configure section, you can see what bundles you have specified:

Configure your bundles

Now, go to the Build section, choose a Build Target, a location where to save the AssetBundle(s) and press Build to create your bundle(s).

Note that the path you specify is relative to the project’s folder.

Build through Script

Using a script has the advantage of automating your tasks. Personally, this is my preferred method. Feel free to reuse and amend the provided scripts.

Note that you need to create those files in an Editor folder as it applies only to the editor and not the player.

First, let’s create checkboxes in the menu to specify the target build:

Android Target checkbox

You need to create 1 file per target.

Next, we add the menu option to build the AssetBundles:

Build AssetBundles menu option

In the example above, I added 2 targets: Android and Windows.

Using an AssetBundle in your code

To use an AssetBundle, you need to download it from Internet and specify which asset you want inside the bundle.

Scene in AssetBundle

To load a scene, you can use the UnityWebRequestAssetBundle class to download it and then use the SceneManager to load it.

Note that LoadScene can be used with the scene name (without the extension) or with the full path (Assets/Scenes/Scene1 in this example).

Downloading and loading a Scene in an AssetBundle

When building the AssetBundle, you will also find a manifest file. If you open it in an editor, you will see the Assets contained in the AssetBundle listed (a convenient way to know how to load the asset using its name):

Assets:
- Assets/Scenes/Scene1.unity

Other Asset in AssetBundle

To load other assets, you need to retrieve the asset based on its type and then use it (instantiate it for a GameObject):

Note that you need to provide the full path in the AssetBundle as well as the extension!

Custom Scripts in AssetBundles

If you some custom code used as a Component in your AssetBundle, it will not be bundled as DLLs or source code is not permitted.

To solve this, you can simply create your code in both the project where your AssetBundle is created and the project where the main project is created. When loading the AssetBundle, if it finds the component in the main project then it will be used. The best way to achieve that is to create a DLL in the main project and then use it in the other project.

--

--

Valéry Raulet

I have been interested in business and technology since I was about 10. My interest spans across so many fields but I hope you’ll find my writing useful!