To get started with AssetBundles, follow these steps. More detailed information about each piece of the workflow can be found in the other pages in this section of documentation.
To assign a given AssetAny media or data that can be used in your game or Project. An asset may come from a file created outside of Unity, such as a 3D model, an audio file or an image. You can also create some asset types in Unity, such as an Animator Controller, an Audio Mixer or a Render Texture. More info
See in Glossary to an AssetBundle, follow these steps:
To read more information on AssetBundle assignments and accompanying strategies, see documentation on Preparing Assets for AssetBundles .
Create a folder called Editor in the Assets folders, and place a script with the following contents in the folder:
using UnityEditor;
using System.IO;
public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles()
{
string assetBundleDirectory = "Assets/AssetBundles";
if(!Directory.Exists(assetBundleDirectory))
{
Directory.CreateDirectory(assetBundleDirectory);
}
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}
}
This script will create a menu item at the bottom of the Assets menu called “Build AssetBundles” that will execute the code in the function associated with that tag. When you click Build AssetBundles a progress bar will appear with a build dialogue. This will take all the assets you labeled with an AssetBundle name in and place them in a folder at the path defined by assetBundleDirectory.
For more detail about what this code is doing, see documentation on Building AssetBundles.
This step is unique to each user and not a step Unity can tell you how to do. If you plan on uploading your AssetBundles to a third party hosting site, do that here. If you’re doing strictly local development and intend to have all of your AssetBundles on disk, skip to the next step.
For users intending to load from local storage, you’ll be interested in the AssetBundles.LoadFromFile API. Which looks like this:
public class LoadFromFileExample extends MonoBehaviour {
function Start() {
var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
if (myLoadedAssetBundle == null) {
Debug.Log("Failed to load AssetBundle!");
return;
}
var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject");
Instantiate(prefab);
}
}
LoadFromFile
takes the path of the bundle file.
If you’re hosting your AssetBundles yourself and need to download them into your game, you’ll be interested in the UnityWebRequest API. Here’s an example:
IEnumerator InstantiateObject()
{
string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;
UnityEngine.__Networking__The Unity system that enables multiplayer gaming across a computer network. [More info](UNetOverview.html)<span class="tooltipGlossaryLink">See in [Glossary](Glossary.html#Networking)</span>.UnityWebRequest request = UnityEngine.__Networking__.UnityWebRequest.GetAssetBundle(uri, 0);
yield return request.Send();
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
GameObject cube = bundle.LoadAsset<GameObject>("Cube");
GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
Instantiate(cube);
Instantiate(sprite);
}
GetAssetBundle(string, int)
takes the uri of the location of the AssetBundle and the version of the bundle you want to download. In this example we’re still pointing to a local file but the string uri could point to any url you have your AssetBundles hosted at.
The UnityWebRequest has a specific handle for dealing with AssetBundles, DownloadHandlerAssetBundle
, which gets the AssetBundle from the request.
Regardless of the method you use, you’ll now have access to the AssetBundle object. From that object you’ll need to use LoadAsset<T>(string)
which takes the type, T
, of the asset you’re attempting to load and the name of the object as a string that’s inside the bundle. This will return whatever object you’re loading from the AssetBundle. You can use these returned objects just like any object inside of Unity. For example, if you want to create a GameObject in the scene, you just need to call Instantiate(gameObjectFromAssetBundle)
.
For more information on APIs that load AssetBundles, see documentation on Using AssetBundles Natively.
Did you find this page useful? Please give it a rating: