Search Unity

Danger: AssetBundle.LoadAllAssets<T>() loads everything.

Discussion in 'Asset Bundles' started by LightStriker, Sep 9, 2019.

  1. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,483
    I should have seen it coming, as Resources has the same issue.

    To be able to figure out the type of an Asset, Unity loads it. It's dumb. That's what headers are for (or manifests), but clearly not in this case.

    So if you have an AssetBundle comprising many assets of many types, trying to retrieve only a specific type will make Unity load everything. The solution is to have AssetBundle "per-type" instead.
     
  2. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    236
    Actual behavior depends on the type of T passed in. If it's a scripting type such as a MonoBehavior or ScriptableObject, it requires loading of that object to know it's specific scripting type. If it's a native type such as Texture2D, it can determine the type without loading the object and thus will not load it if it is not that type or does not derive from that type.
     
    Last edited: Sep 10, 2019
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,483
    Honestly, that should be a bold red warning in the documentation. Having a 250Mb+ AssetBundle and finding out it just loads everything is not a pleasant surprise ;)

    Honestly, I keep saying it, but there should be more data in the headers, such as typing.
     
  4. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    236
    Type information is already contained in the headers. The exception is scripting types due to how script instance data is stored. The only case where everything is loaded by LoadAllAssets<T> is if every asset in that bundle is a ScriptableObject. Looking at the source, I think we can add a few more checks to reduce this further, such as if T is not a scripting type. I've added a note to also improve the documentation for the Load* asset bundle pages.