After building the Asset Bundles we have to use them.There are two ways to download an AssetBundle: Non-caching: This is done by creating a new WWW object. These AssetBundles are not cached to Unity's Cache folder. Caching: This is done by creating the WWW.LoadFromCacheOrDownload call. The AssetBundles are cached to Unity's Cache folder in the local storage device.
WebPlayer shared cache up to 50 MB of cached AssetBundles.
PC/Mac Standalone applications and iOS/Android applications shared cache upto 4 GB.
Example of Non- Catching 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));
bundle.Unload(false);
}
}
}
In most of the cases we use WWW.LoadFromCacheOrDownload to download Asset Bundles so here is an example of Caching:
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 ()
{
while (!Caching.ready)
yield return null;
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));
bundle.Unload(false);
}
}
}
0 Comment(s)