此部分的前提是您已了解资源包构建方法。如不了解上述内容,请参阅构建资源包 (Building AssetBundles)
有两种方法可供下载资源包 (AssetBundle)
以下是一个非缓存下载示例:
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)); // Unload the AssetBundles compressed contents to conserve memory bundle.Unload(false); } } }
建议使用 WWW.LoadFromCacheOrDownload 下载资源包 (AssetBundles)。例如:
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)); // Unload the AssetBundles compressed contents to conserve memory bundle.Unload(false); } } }
访问 .assetBundle
属性时,程序将提取下载的数据并创建资源包 (AssetBundle) 对象。此时,可加载资源包中的对象。传递到 LoadFromCacheOrDownload 的第二个参数指定要下载的资源包 (AssetBundle) 版本。如果缓存中没有该资源包 (AssetBundle),或拥有的版本低于所需版本,LoadFromCacheOrDownload 将下载资源包 (AssetBundle)。否则,将从缓存加载资源包 (AssetBundle)。
准备好组件后,就可构建场景,以便加载资源包 (AssetBundle) 并在屏幕上显示其内容。
最终工程结构
首先,转至 file://
,例如 file://C:/UnityProjects/AssetBundlesGuide/Assets/AssetBundles/Cube.unity3d
现在可在编辑器 (Editor) 中单击播放,然后应看见从资源包 (AssetBundle) 加载的立方体 (Cube) 预设。
使用需要构建并加载资源包 (AssetBundles) 的编辑器 (Editor) 可延缓开发进程。例如,如果修改了资源包 (AssetBundle) 中的资源 (Asset),则需稍后重新构建该资源包,并且在生产环境中,很可能会一起构建所有资源包 (AssetBundles),使得单个资源包的更新过程冗繁漫长。一个更有效的方法是,在编辑器 (Editor) 中设置一个独立的代码路径,可直接加载该资源 (Asset),而无需从资源包 (AssetBundle) 加载。为此,需要使用 Resources.LoadAssetAtPath(仅限编辑器)。
// C# Example // Loading an Asset from disk instead of loading from an AssetBundle // when running in the Editor using System.Collections; using UnityEngine; class LoadAssetFromAssetBundle :MonoBehaviour { public Object Obj; public IEnumerator DownloadAssetBundle<T>(string asset, string url, int version) where T :Object { Obj = null; #if UNITY_EDITOR Obj = Resources.LoadAssetAtPath("Assets/" + asset, typeof(T)); yield return null; #else // Wait for the Caching system to be ready while (!Caching.ready) yield return null; // Start the download using(WWW www = WWW.LoadFromCacheOrDownload (url, version)){ yield return www; if (www.error != null) throw new Exception("WWW download:"+ www.error); AssetBundle assetBundle = www.assetBundle; Obj = assetBundle.Load(asset, typeof(T)); // Unload the AssetBundles compressed contents to conserve memory bundle.Unload(false); } #endif } }
Page last updated: 2013-06-26