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.
Note: This section describes the creation of AssetBundles using the built-in BuildPipeline.BuildAssetBundles() API. A recommended, and more user friendly, alternative is to use the Addressables package.
To assign a given Asset to an AssetBundle, follow these steps:
/
. For example, use the AssetBundle name environment/forest
to create a bundle named forest
under an environment
sub-folderNote: In the Inspector you can assign an AssetBundle to a folder in your Project. By default, all Assets in that folder are assigned to the same AssetBundle as the folder. The AssetBundle assignments for individual Assets takes precedence, however.
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 creates a menu item at the bottom of the Assets menu called Build AssetBundles that executes the code in the function associated with that tag. When you click Build AssetBundles a progress bar appears with a build dialog. This takes all the Assets you labeled with an AssetBundle name and places them in a folder at the path assetBundleDirectory
defines.
Note: You can also define the content of AssetBundles programmatically instead of using the Inspector-based method described above. This is available by using a signature of BuildPipeline.BuildAssetBundles that accepts an array of AssetBundleBuild structures. In that case the list of desired Assets for each bundle are passed in, and any assignment to AssetBundles made in the Inspector is ignored.
For more details about this, see documentation on Building AssetBundles.
If you want to load from local storage, use the AssetBundles.LoadFromFile
API, which looks like this:
public class LoadFromFileExample : MonoBehaviour {
void 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 your AssetBundles are hosted online, or you are running on a platform that does not support direct file system access, then use the UnityWebRequestAssetBundle
API. Here’s an example:
IEnumerator InstantiateObject()
{
string url = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;
var request
= UnityEngine.Networking.UnityWebRequestAssetBundle.GetAssetBundle(url, 0);
yield return request.Send();
AssetBundle bundle = UnityEngine.Networking.DownloadHandlerAssetBundle.GetContent(request);
GameObject cube = bundle.LoadAsset<GameObject>("Cube");
GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
Instantiate(cube);
Instantiate(sprite);
}
GetAssetBundle(string, int)
takes the URL of the location of the AssetBundle and the version of the bundle you want to download. This example points to a local file, but string url
could point to any URL you have your AssetBundles hosted at.
The UnityWebRequestAssetBundle class has a specific handle for dealing with AssetBundles, DownloadHandlerAssetBundle
, which gets the AssetBundle from the request.
Regardless of the method you use, you now have access to the AssetBundle object. From that object you 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 returns 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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.