Unity-不要两次加载相同的资产捆绑 [英] Unity - don't load the same assetbundle twice

查看:523
本文介绍了Unity-不要两次加载相同的资产捆绑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下载资产捆绑包时遇到问题.

I am having problems with downloading assetbundles.

我得到了错误:

The asset bundle 'url/levelpackIphone-Copy.unity3d' can't be loaded because
another asset bundle with the same files are already loaded

我需要检查资产捆绑包是否已经下载.我的问题是我不能仅存储URL,因为下载的文件可以具有不同的名称,但仍包含相同的内容.

I need to check if the asset bundle has already been downloaded. My problem is that I cannot just store the URL, because the file you download can have a different name, but still contain the same content.

因此,我需要以某种方式检查资产捆绑包是否已经下载. 我真的不喜欢使用

Thus somehow I need to check whether the asset bundle already has been downloaded. I don't really like to use something like

bundle.Unload(false);

推荐答案

我在统一引发以下错误时遇到了同样的问题:

I was getting the same problem where unity was throwing the following error:

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

Cannot load cached AssetBundle. A file of the same name is already loaded from another AssetBundle

我当时在关卡的不同阶段加载资产束,但是当我尝试重新启动关卡并尝试再次加载资产束时,遇到了与上述相同的错误.然后,我尝试将资产捆绑包保存在列表中,并尝试在级别重新启动时清除列表,但这并不能解决问题.我什至尝试Caching.CleanCache()从内存中删除任何数据,但是没有用.在将资产束下载并在级别加载后再次从磁盘加载之后,我将资产束保存在磁盘上.

I was loading asset bundles at different stages of level but when I try to restart the level and try to load the asset bundle again I got the same error stated above. Then I tried saving the asset bundles in a list and trying to clear the list at level restart but that doesn't solved the problem. I even tried Caching.CleanCache() to remove any data from the memory but no use. I was saving the assets bundles on the disk after downloading and loading them again from the disk after level load.

解决方案:

private IEnumerator LoadAssetFromFile(string _name) 
{ 
    print("Came inside LoadAssetFromFile :" + _name); 
    WWW www= new WWW(string.Concat("file:///", Application.dataPath, "/", _name +".unity3d"));//(m_savePath + _name); 
    //WWW www= new WWW(string.Concat(Application.dataPath, "/", _name));//(m_savePath + _name);

    yield return www;

    if(www.error == null)
    {
        //m_bundle =  www.assetBundle;
        AssetBundle bundle =  www.assetBundle;
        www.Dispose();
        www = null;
        string path = m_savePath + _name;
        //object [] obj = m_bundle.LoadAll();
        //for(int i=0; i< obj.Length; i++)
        //{
            //print ("Obj :"+ i +" " + obj[i].ToString());
        //}
        bundle.LoadAll();
        print ("AssetBundle is :" + bundle.mainAsset);
        print("============> " + path);
        GameObject _go =  (GameObject)Instantiate(bundle.mainAsset);
        bundle.Unload(false);
    }
    else
    {
        return false;
    }   
}

实例化gameObject后使用bundle.Unload(false)可以解决问题.

Using bundle.Unload(false) after instantiating the gameObject have done the trick.

这篇关于Unity-不要两次加载相同的资产捆绑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆