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
- 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.
- 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() { // Download the file from the URL. It will not be saved in the Cache 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; void Start() { StartCoroutine (DownloadAndCache()); } IEnumerator DownloadAndCache (){ // Wait for the Caching system to be ready while (!Caching.ready) yield return null; // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache 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 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.
Page last updated: 2012-07-23