DijkeMark
DijkeMark

Reputation: 1306

Unity - don't load the same assetbundle twice

I am having problems with downloading assetbundles.

I get the error:

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

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);

Upvotes: 2

Views: 16442

Answers (1)

Jeeteshb
Jeeteshb

Reputation: 101

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

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

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.

Solution:

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;
    }   
}

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

Upvotes: 2

Related Questions