首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法加载缓存的AssetBundle。同名的文件已经从另一个AssetBundle加载。

无法加载缓存的AssetBundle。同名的文件已经从另一个AssetBundle加载。
EN

Stack Overflow用户
提问于 2013-06-05 11:37:02
回答 1查看 5.3K关注 0票数 1

我试图加载一个场景,但是我从标题中得到了错误,我只是不知道为什么,因为我在AssetBundle上调用Unload(false)。有人能帮我吗?谢谢。

代码语言:javascript
复制
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;

            bundle.LoadAll();

            AsyncOperation async = Application.LoadLevelAsync("main");
            Debug.Log (async.progress);
            yield return async;

            bundle.Unload(false);
        }
}
EN

回答 1

Stack Overflow用户

发布于 2016-05-31 13:53:15

如果不想使用Unload()函数,则在使用资产包之后清除缓存:-

代码语言:javascript
复制
Caching.CleanCache();

而且每件事情都会正常工作,但是每次在使用包后清除缓存时,您都必须下载资产包。

或者你可以这样做

首先调用DoNotDestroyOnLoad() (用于将引用保存在Start()函数中),并在使用WWW.LoadFromCacheOrDownload下载资产时创建一个静态变量来存储资产包的引用,将引用分配给静态变量并在使用后卸载资产。如果您不卸载资产包并再次使用WWW.LoadFromCacheOrDownload,则将引发与您声明相同的错误。

假设您使用资产包加载场景,那么在退出场景之前,卸载存储在该静态变量中的资产包引用。这就是为什么使用静态变量,以便我们可以从任何脚本访问它,并在任何时候卸载它,我们也要小心版本。

代码语言:javascript
复制
class LoadScene:MonoBehaviour{
 ***public static AssetBundle refrenceOfAsset;***
 private AssetBundle assetBundle;

void Start(){
***DoNotDestroyOnLoad(gameObject);*** }
protected IEnumerator LoadTheScene()
{
    if (!Caching.IsVersionCached(url, version)){
        WWW www = WWW.LoadFromCacheOrDownload(url, version);
        yeild return www;  assetBundle = www.assetBundle;
        ***refrenceOfAsset = assetBundle;*** 
        www.Dispose();
        // Do what ever you want to do with the asset bundle but do not for get to unload it 
        refrenceOfAsset.Unload(true);
        }
    }
    else{
        Debug.Log("Asset Already Cached...");
        if(refrenceOfAsset!=null)
        refrenceOfAsset.Unload(true);
    }

}

或者你也可以访问统一http://docs.unity3d.com/Manual/keepingtrackofloadedassetbundles.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16938903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档