Using AssetBundles in your next game or application.

Ekansh Mittal
DreamXR
Published in
7 min readJun 9, 2021
Header Image

Hello Folks 🙋‍♂️,

I am an AR Evangelist and have been exploring this field for quite some time. Recently, I got the chance to work on AssetBundles in Unity. This is the first time I was introduced to this term and I have no idea as to how to create AssetBundles and integrate them with the server. It was an amazing experience and I was blown away by the possibilities it brings to the table for the developers who are building any game or app.

I was really excited to deep dive into this and figure out how it works, and well it was a rollercoaster ride filled with its ups and downs but in the end, the journey was amazing and fruitful in all essence.

Through this blog, I will be sharing how we can create Asset Bundles for Android and iOS.

Let’s first understand what are Asset Bundles and what are the functionalities and use cases of Asset Bundles and then we will be learning how to create Asset Bundles in Unity.

What are Asset Bundles?
An AssetBundle is used for content that is stored separately from a main game or application and loaded at runtime. This helps the developers use high graphics environments for their game and not worry about the graphic's size. This also helps the customers to install only the parts that they need.
One important thing to keep in mind is that they are archive files that contain platform-specific non-code Assets (such as Models, Audio Clips, Animation Tracks, and even entire scenes). That means you can create asset bundles for even a single audio clip, animation tracks, and even your whole scene. Isn’t that amazing?

Use cases of Asset Bundles
They can be used to update or add content post-release of the app without zero or minimal changes in the current code structure.
It also allows automatic updates and the content can be reused from project to project.
If somehow your asset bundle is not working properly, you just need to create a new asset bundle without altering the code and unity structure of your app.

So, now we get a basic idea of what exactly are asset bundles and their use cases. let’s take a closer look at how to create asset bundles?

We will be creating a general script to create asset bundles for both Android and iOS platforms with one click. Let’s cut the chit-chat and get started with it.

Steps To Complete

  1. Project Setup
  • Creating a new Unity Project in 2019.4.x.
  • Creating a prefab.
  • Creating a basic scene in Unity.

2. Working on creating Asset Bundle

  • Writing a general script to build asset bundles.
  • Storing Asset Bundles in Local Storage.

Project Setup

In this part, we will be creating a new Unity project and setting the basics required for creating asset bundles.

Creating a new Unity Project

Simply create a new project in Unity (use Unity 2019.3.x) with a 3D Template and name it whatever you want to, I will be naming it “AssetBundle_Creation”.

Create new project named AssetBundle_Creation
Create a new project named AssetBundle_Creation

Now that our newly created project has been initialized, let me change the layout of my editor to “Tall” because that is how I like it.
A quick suggestion from my side to all the Unity beginners to change your Unity Layout to “Tall” if you are a beginner in Unity so that you can follow my steps easily.

Changing the layout to tall to work easily in Unity.

Let us also create a good folder structure so that things don’t get messy afterward. We will make a folder named “MyProject” which will contain all the scenes, scripts, prefabs, etc created by us.

Creating a clean folder structure
Creating a clean folder structure

Creating a Prefab

Now, let’s create a basic Scene so that we can use this scene to create a prefab. Open your “Scenes” folder we created in the “MyProject” folder and create a new scene and name it “AssetBundleScene” (You can name your scene anything you want to, it won’t affect anything).

Creating a basic Scene
Creating a basic scene

After we had created a scene, let’s add a 3D object to our scene. Right-click on the scene panel and add a 3D object and let’s add a capsule.

Create a 3D object
Create a 3D Object

Let’s also add color to our capsule. Go to the “Materials” Folder we created and create a new Material and assign your favorite color to the Material. I will assign Blue to the material. After that, just add this material to your 3D object, and voila, now your 3D object is colored.

Adding color to the capsule
Add your favorite color to the capsule

After this is done, let’s just move our capsule to the “Prefabs” folder, so we created a prefab of the 3D object.

Creating a prefab of the 3D object
Creating a prefab of the object

For beginners who are new to Unity, A Prefab acts as an asset template that allows fully configured GameObjects to be saved in the Project for reuse. It helps and saves a lot of time while we are building an application or game that requires the same set of parameters of the object again and again.

Once you’re done with this, we have completed this part.

The first part of the blog is done 😎.

Creating Asset Bundles

Step #1:
Go to the “Prefabs” folder and click on the prefab we created and stored in the first part of the blog. After clicking on it, you will see some properties that appear on the Inspector Panel (Rightmost Panel). In the Inspector Panel, at the bottom you will see the prefab, there below is an option to name the asset bundle. Name your asset bundle and hit enter to save it.
If it sounds complicated, you can see the GIF below and follow the steps.

Create a assetbundle name
Create an asset bundle name

Step #2:
Create an “Editor” folder under “MyProject” and create a new C# script inside it. Let’s name this script “creatingBundles”. Make sure to not give any space between the name of the script.

Creating a script for building asset bundles
Creating a script for building asset bundles

You might be wondering, why we put the script inside the “Editor” folder and not in the usual “Scripts” folder, right?
The reason is simple because we are adding the functionality of Building AssetBundles to the Unity Editor so that with one click we will be able to create asset bundles hassle-free.

Step #3:
Now, let’s start with the scripting part. Don’t worry the script is simple and it's easy to understand. I had added the necessary comments to help you understand the flow and use of this code. To make sure this script works, we need to put it in the editor folder, so that unity recognizes that it is using the Editor class and it can be implemented.

using UnityEditor;
using UnityEngine;
using System.IO; //Added to create folders in Unity.
public class creatingBundles
{
//Used to create a menu item called Build AssetBundles to
//build asset bundles directly using this.
[MenuItem("Assets/Build AssetBundles")]
static void BuildAssetBundles()
{
string assetBundleDirectory = "Assets/AssetBundles";
if(!Directory.Exists(assetBundleDirectory)
{
//if there is no folder/directory of this name, then creating
//one using this piece of code.
Directory.CreateDirectory(assetBundleDirectory);
}
//Used to build the asset bundles and save them in that
//particular folder.
BuildPipeline.BuildAssetBundles(assetBundleDirectory,
BuildAssetBundleOptions.None,
BuildTarget.Android);
}
}

This is all the code we need to build AssetBundles. Now, save changes and close your script editor.

Creating and saving the script
Creating and saving the script

Before moving onto the last step, let's check some different parameters that are present as well and you can use them according to your use case.

BuildAssetBundleOptions comes with four parameters.

Also, there are various target platforms for which we can create asset bundles specifically. Here, is the list of target platforms.

These images are taken from Unity Learn.

Step #4:
After we are done with this, go to unity and in the project window, right-click and choose Build AssetBundles that is visible at the Menu bar. It will automatically create an AssetBundle folder and Save your asset bundles in there.

Creating asset bundles
Creating asset bundles

We have finally and successfully created the asset bundles in Unity and now you can use them as you want.

Asset Bundles created successfully.
Asset bundle is created successfully

Ending Notes

At the end of this blog, I hope you got to learn a thing or two. I will be creating another blog to explain how to upload these bundles to a server and fetch them directly from the server. I am attaching the GitHub repo link of the complete project. Feel free to download it make changes according to your needs.

https://github.com/Yaugan/AssetBundle

Feel free to connect with me on LinkedIn.

Kudos to everybody

-Yaugan
(Ekansh Mittal)
XR Enthusiast

--

--

Ekansh Mittal
DreamXR
Writer for

An introvert person, starting writing about simple topics which affect many people around us. Always up for improvements and suggestions.