Downloading Asset Bundles
Manual     Reference     Scripting   
Unity Manual > Advanced > AssetBundles (Pro only) > Downloading Asset Bundles

Downloading Asset Bundles

Downloading AssetBundles

This section assumes you already learned how to build asset bundles. If you have not, please see Building AssetBundles

There are two ways to download an AssetBundle

  1. Non-caching: This is done using a creating a new WWW object. The AssetBundles are not cached to Unity’s Cache folder in the local storage device.
  2. Caching: This is done using the WWW.LoadFromCacheOrDownload call. The AssetBundles are cached to Unity’s Cache folder in the local storage device. The WebPlayer shared cache allows up to 50 MB of cached AssetBundles. PC/Mac Standalone applications and iOS/Android applications have a limit of 4 GB. WebPlayer applications that make use of a dedicated cache are limited to the number of bytes specified in the caching license agreement.

Here's an example of a non-caching download:

using System;
using UnityEngine;
using System.Collections; class NonCachingLoadExample : MonoBehaviour {
   public string BundleURL;
   public string AssetName;
   IEnumerator Start() {
	   using (WWW www = new WWW(BundleURL)) {
		   yield return www;
		   if (www.error != null)
			   throw new Exception("WWW download had an error:" + www.error);
		   AssetBundle bundle = www.assetBundle;
		   if (AssetName == "")
			   Instantiate(bundle.mainAsset);
		   else
			   Instantiate(bundle.Load(AssetName));
	   }
   }
}

The recommended way to download AssetBundles is to use WWW.LoadFromCacheOrDownload. For example:

using System;
using UnityEngine;
using System.Collections;

public class CachingLoadExample : MonoBehaviour {
	public string BundleURL;
	public string AssetName;
	public int version;
	IEnumerator Start() {
		using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
			yield return www;
			if (www.error != null)
				throw new Exception("WWW download had an error:" + www.error);
			AssetBundle bundle = www.assetBundle;
			if (AssetName == "")
				Instantiate(bundle.mainAsset);
			else
				Instantiate(bundle.Load(AssetName));
		}
	}
}

When you access the .assetBundle property, the downloaded data is extracted and the AssetBundle object is created. At this point, you are ready to load the objects contained in the bundle. The second parameter passed to LoadFromCacheOrDownload specifies which version of the AssetBundle to download. If the AssetBundle doesn't exist in the cache or has a version lower than requested, LoadFromCacheOrDownload will download the AssetBundle. Otherwise the AssetBundle will be loaded from cache.

Putting it all together

Now that the components are in place you can build a scene that will allow you to load your AssetBundle and display the contents on screen.

Final project structure

First create an empty game object by going to GameObject->CreateEmpty. Drag the CachingLoadExample script onto the empty game object you just created. Then type the the URL of your AssetBundle in the BundleURL field. As we have placed this in the project directory you can copy the file directory location and add the prefix file://, for example file://C:/UnityProjects/AssetBundlesGuide/Assets/AssetBundles/Cube.unity3d

You can now hit play in the Editor and you should see the Cube prefab being loaded from the AssetBundle. This is the end of the basic example of how to build and use an AssetBundle.

back to AssetBundles Intro

Page last updated: 2012-05-02