前几天看的AssetBundle的使用,今天把它先整理一下,在unity3d中的资源的打包和加载基本都是通过AssetBundle 来实现的,
AssetBundle配合WWW一起实现从服务器端或者内存中动态加载资源,下面是我看刘国柱老师书借鉴着写的一段AssetBundle加载本地中
资源的例子。代码部分如下:
using UnityEngine; using System.Collections; public class Load01 : MonoBehaviour { //首先加载总的manifest文件 private string manifestName ="StreamingAssets"; //加载assetsBudles文件 private string assetBundleName = "test"; IEnumerator Start() { Debug.Log(Application.dataPath); //{ // //WWW www = new WWW("http://baike.baidu.com/"); // WWW www = WWW.LoadFromCacheOrDownload("http://www.baidu.com/",5); // yield return www; // Debug.Log(www.GetType().ToString()); //AssetBundle所在的路径 string assetBundlePath = "file:D:/Lianxi_File/Unity/Message/MessageDemo/Assets/StreamingAssets/"; ////string assetBundlePath = "file:D:/Lianxi_File/Unity/Message/MessageDemo/Assets/StreamingAssets/StreamingAssets"; //manifest 文件的路径 string manifestPath = assetBundlePath + manifestName; //string manifestPath="file:D:/Lianxi_File/Unity/Message/MessageDemo/Assets/StreamingAssets/StreamingAssets"; //首先加载manifest文件 WWW wwwManifest = WWW.LoadFromCacheOrDownload(manifestPath,0); yield return wwwManifest; //判断是否加载到资源 if(!string.IsNullOrEmpty(wwwManifest.error)) //wwwManifest.error==null { Debug.Log(wwwManifest.error); yield break; } else { AssetBundle manifestBundles = wwwManifest.assetBundle; AssetBundleManifest manifest = (AssetBundleManifest)manifestBundles.LoadAsset("AssetBundleManifest"); manifestBundles.Unload(false); //获取依赖文件列表 string[] dependentAssetBundles = manifest.GetAllDependencies(assetBundleName); AssetBundle[] abs = new AssetBundle[dependentAssetBundles.Length]; Debug.Log(dependentAssetBundles.Length); for (int i = 0; i < dependentAssetBundles.Length;i++ ) { //加载所有的依赖文件(第一次加载) WWW www1 = WWW.LoadFromCacheOrDownload(assetBundlePath+dependentAssetBundles[i],0); yield return www1; Debug.Log("1111"); } //加载需要的文件(从缓存中加载出来,第二次加载) WWW www2 = WWW.LoadFromCacheOrDownload(assetBundlePath+assetBundleName,0); yield return www2; AssetBundle assetBundle = www2.assetBundle; Debug.Log(assetBundle.ToString()); Debug.Log("finish"); } } }
以上是写的加载的例子,过程中遇到一些问题,后续会更加完善
........未完待续