个人资料

主页 博客 留言板 搜索
跳过导航链接首页 > 博客列表 > 博客正文

unity加载存在依赖关系的AssetBundle文件代码(五)

分类

接上文,Unity加载AssetBundle包(四)

unity加载存在依赖关系的AssetBundle文件时,先加载被依赖项,然后加载依赖项。举个例子,一个AssetBundle名字为“test”,依赖另一个AssetBundle的名字为“m1”,加载test之前应该先加载manfest文件查找配依赖项m1先行加载.示例代码如下所示:

public string manifestName="AssetBundles";
public string assetBundleName = "test";
IEnumerator Start()
{
    string assetBundlePath = "file://" + Application.dataPath + "/AssetBundles/";
    //manifest所在目录
    string manifestPath = Application.dataPath + manifestName;
 
    //首先加载Manifest文件
    WWW wwwManifest = WWW.LoadFromCacheOrDownload(manifestPath, 0);
    yield return wwwManifest;
    if (wwwManifest.error==null)
    {
        AssetBundle manifestBundle = wwwManifest.assetBundle;
        AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
        manifestBundle.Unload(false);
 
        //获取依赖文件列表
        string[] dependentAssetBundles = manifest.GetAllDependencies(assetBundleName);
        AssetBundle[] abs = new AssetBundle [dependentAssetBundles.Length];
        for (int i = 0; i < dependentAssetBundles.Length; i++)
        {
            //加载所有的依赖文件
            WWW www = WWW.LoadFromCacheOrDownload(assetBundlePath+dependentAssetBundles[i], 0);
            yield return www;
            abs[i] = www.assetBundle;
        }
        WWW www2 = WWW.LoadFromCacheOrDownload(assetBundlePath+assetBundleName,0);
        yield return www2;
        AssetBundle assetBundle = www2.assetBundle;
    }
    else
    {
        Debug.Log(wwwManifest.error);
    }
}

下一篇:从AssetBundle对象中获取Assets(六)

songshizhao
最初发表2017/3/15 23:33:25 最近更新2017/3/15 23:33:25 4980
为此篇作品打分
10