Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

The AssetBundle can't be loaded

Discussion in 'Scripting' started by ARares, Mar 13, 2017.

  1. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    When i start the game the AssetBundle is working fine, but when i go in a other scene and i come back i have that error:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class UpdateGame : MonoBehaviour {
    6.  
    7.     public string bundleUrl;
    8.     public RawImage lastVersion;
    9.  
    10.     AssetBundle assetBundle;
    11.     int correctAnswer;
    12.  
    13.     void Start()
    14.     {
    15.         LastVersionN(1);
    16.     }
    17.  
    18.     public void LastVersionN (int questNumber)
    19.     {
    20.         if(!assetBundle)
    21.         {
    22.             StartCoroutine(LoadAssetBundle(questNumber));
    23.             return;
    24.         }
    25.         StartCoroutine(ShowVersion(questNumber));
    26.     }
    27.  
    28.     IEnumerator LoadAssetBundle (int questNumber)
    29.     {
    30.         WWW www = new WWW(bundleUrl);
    31.         yield return www;
    32.         assetBundle = www.assetBundle;
    33.         LastVersionN(questNumber);
    34.     }
    35.  
    36.     IEnumerator ShowVersion (int questNumber)
    37.     {
    38.         AssetBundleRequest assetTexture = assetBundle.LoadAssetAsync<Texture2D>("LV" + questNumber);
    39.         yield return assetTexture;
    40.         lastVersion.texture = assetTexture.asset as Texture2D;
    41.     }
    42. }
    43.  
     
  2. Emiles

    Emiles

    Joined:
    Jan 22, 2014
    Posts:
    61
    When you load an asset bundle it stays in memory so essentially you don't need to load it again.

    You can either save a reference to it, so you can check if you e loaded it already, or you can unload it after you've created the assets you need to load.

    You may gut also consider keeping a reference to assets you already have instances. So you could clone those rather than having to load the asset bundle again.

    All of this is something you have to write code for.
     
  3. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    How i can check if the loaded bundle is already loaded?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  5. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I tried your link, but i have the same error and 2 more:
    Error.png

    Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class UpdateGame : MonoBehaviour {
    6.  
    7.     public string bundleUrl;
    8.     public RawImage lastVersion;
    9.  
    10.     AssetBundle assetBundle;
    11.     int correctAnswer;
    12.  
    13.     void Start()
    14.     {
    15.         LastVersionN(1);
    16.     }
    17.  
    18.     public void LastVersionN (int questNumber)
    19.     {
    20.         if(!assetBundle)
    21.         {
    22.             StartCoroutine(LoadAssetBundle(questNumber));
    23.             return;
    24.         }
    25.         StartCoroutine(ShowVersion(questNumber));
    26.     }
    27.  
    28.     IEnumerator LoadAssetBundle (int questNumber)
    29.     {
    30.         while (!Caching.ready)
    31.             yield return null;
    32.  
    33.         WWW www = WWW.LoadFromCacheOrDownload(bundleUrl, 5);
    34.         yield return www;
    35.  
    36.         if (!string.IsNullOrEmpty(www.error))
    37.         {
    38.             Debug.Log(www.error);
    39.             yield return null;
    40.         }
    41.  
    42.         assetBundle = www.assetBundle;
    43.  
    44.         var asset = assetBundle.mainAsset;
    45.  
    46.         //WWW www = new WWW(bundleUrl);
    47.         //yield return www;
    48.         //assetBundle = www.assetBundle;
    49.         LastVersionN(questNumber);
    50.     }
    51.  
    52.     IEnumerator ShowVersion (int questNumber)
    53.     {
    54.         AssetBundleRequest assetTexture = assetBundle.LoadAssetAsync<Texture2D>("LV" + questNumber);
    55.         yield return assetTexture;
    56.         lastVersion.texture = assetTexture.asset as Texture2D;
    57.     }
    58. }
    59.  
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Then you'll have to keep a reference that survives scene loads or unload it before changing scenes.
     
  7. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    In original code, where i need to put unload line?, i don't know much about Assets Bundles.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'd first try doing it in OnDestroy - so when the object gets destroyed because of the scene change it unloads the bundle.
     
  9. NareshKhandla

    NareshKhandla

    Joined:
    Aug 6, 2013
    Posts:
    28
    I have resolved this problem using add below lines,

    private AssetBundle myLoadedAssetBundle;
    string bundleUrl="";
    IEnumerator LoadAssetBundle ()
    {
    if (myLoadedAssetBundle != null) {
    myLoadedAssetBundle.Unload(false); //scene is unload from here
    }

    while (!Caching.ready)
    yield return null;

    WWW www = WWW.LoadFromCacheOrDownload(bundleUrl, 1);
    yield return www;

    if (!string.IsNullOrEmpty(www.error))
    {
    Debug.Log(www.error);
    yield return null;
    }
    myLoadedAssetBundle = www.assetBundle;
    }
     
  10. AmobearGame

    AmobearGame

    Joined:
    Mar 31, 2016
    Posts:
    28
    Hi I tried everthing like as myLoadedAssetBundle.Unload(false) after Instantiate but it does't works.the first time Instantiate does work but then I reload scene (Run LoadFromCacheOrDownload again) received error :
    "can't be loaded because another assetbundle with the same files is already loaded."
    Please for me solutions .Thanks.
     
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    You can't load the same asset bundle again, nor you can have two identically named asset bundles loaded at the same time. If bundle is already loaded - just use it. If you have multiple bundles with the same name, you have to unload the bundle then load a different one.
    See documentation for AssetBundle class on how to get loaded bundles and unload them.
     
  12. AmobearGame

    AmobearGame

    Joined:
    Mar 31, 2016
    Posts:
    28
    @Aurimas-Cernius Thanks.I used this method and it's doest work well.
    AssetBundle.UnloadAllAssetBundles(true);
     
  13. ChristyJamesUST1

    ChristyJamesUST1

    Joined:
    Jun 26, 2019
    Posts:
    2
    @petvenger @Aurimas-Cernius I have tried AssetBundle.UnloadAllAssetBundles(true); but not working. Whats the proper way to unload all loaded AssetBundles.
     
  14. mathewsbabu

    mathewsbabu

    Joined:
    Sep 30, 2014
    Posts:
    33
    @Aurimas-Cernius
    Hi,
    I am using "AssetBundle.GetAllLoadedAssetBundles()" to load already loaded AssetBundles.It loads normal AssetBundles but it doesn't load scene bundles.Any idea??
     
  15. bhallion

    bhallion

    Joined:
    Mar 26, 2018
    Posts:
    5
    Hi,

    In my case for Scene Asset Bundles. I replaced my AssetBundle.GetAllLoadedAssetBundles() by a custom List I manage myself.
    I put in the list every AssetBundle I'm loading.
    and before each Load I check the list to see if the bundle is still present.
    The list might contains some 'null' items. (I presume they are unloaded AssetBundles)
    Make sure to clean up 'null' items in the list.

    Code (CSharp):
    1.         private static Dictionary<string, AssetBundle> loadedAssetBundleMap = new Dictionary<string, AssetBundle>();
    2.  
    3.        public static AssetBundle GetAssetBundle(string assetBundleName)
    4.        {
    5.            foreach (var kv in loadedAssetBundleMap)
    6.                if (kv.Key == assetBundleName)
    7.                    return kv.Value;
    8.            return null;
    9.        }
    10.  
    11.         public AssetBundle AssetBundle
    12.         {
    13.             get
    14.             {
    15.                 AssetBundle bundle = GetAssetBundle(assetBundleName);
    16.  
    17.                 if (bundle == null)
    18.                 {
    19.                     bundle = AssetBundle.LoadFromMemory(_bytes);
    20.                     loadedAssetBundleMap[assetBundleName] = bundle;
    21.                 }
    22.                 return bundle;
    23.             }
    24.         }
     
    CasualT_Bossfight likes this.
  16. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,140
    This still does not seem well handled or explained by Unity after UnityWebRequest has replaced WWW.

    This error is thrown out of the blue, it isn't explained fully or in the manual as far as I can see. The relevant function (DownloadHandlerAssetBundle.GetContent) should return a result with an error code/message, not have the runtime log an error!

    Even if you do your best to avoid duplicate bundle loads it seems if the bundle contents are the same - maybe just by names?, but a different file, it can still throw the error (what is the check?). This can be a problem in some art cases.
    Either way the error should be detailed and you should be able to handle it - catch it.
     
    Last edited: May 26, 2020
  17. Julien-Lynge

    Julien-Lynge

    Joined:
    Nov 5, 2010
    Posts:
    142
    I'm in the same boat as @andyz: in my code, I need to be able to handle this error. I've tried to wrap it in a try-catch, but that doesn't do anything. And there doesn't seem to be any way to query what bundles are currently loaded.

    I'm downloading a bunch of asset bundles via concurrent coroutines. After I load the asset needed from the bundle, I immediately call bundle.Unload(false);. However, if I happen to be downloading the same bundle twice, in two different coroutines, I'll sometimes get this error message. Since coroutines are all happening on the main thread, this means that there is some amount of time that bundles remain persistent in memory after calling bundle.Unload. So there doesn't seem to be any way to avoid this bug.

    @Aurimas-Cernius, any ideas for workarounds?
     
  18. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    Well, that is your problem.
    You have to manage things in a way that you don't load the same asset bundle twice. You can't do random things in random places and expect consistent behavior.
     
  19. Julien-Lynge

    Julien-Lynge

    Joined:
    Nov 5, 2010
    Posts:
    142
    Right, I've already been working on that part. But we should be able to load an asset bundle, unload it, and then load it again. And, really, the bigger issue is that this error is written to the console but produces no exception that we can catch and handle ourselves. As @andyz mentioned:

    "This error is thrown out of the blue, it isn't explained fully or in the manual as far as I can see. The relevant function (DownloadHandlerAssetBundle.GetContent) should return a result with an error code/message, not have the runtime log an error!"

    Is that issue captured anywhere on the roadmap?
     
  20. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
  21. Julien-Lynge

    Julien-Lynge

    Joined:
    Nov 5, 2010
    Posts:
    142
    That's awesome - thanks! Hopefully that will be useful for @andyz and others too.
     
  22. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,140
    That's good, can I check what the test for duplicate bundles is that throws the error?
    Is it a hash code for the entire bundle contents, not anything to do with the item names inside (which can be same in each bundle)?