Search Unity

LoadAllAssets from bundle is slow

Discussion in 'Asset Bundles' started by mikest, Jul 22, 2019.

  1. mikest

    mikest

    Joined:
    Feb 23, 2015
    Posts:
    29
    I've recently converted my game over to using Asset Bundles from Resources.Load as I prep it for console. During the initial load of the game, I load each bundle into a Byte array and load it into memory

    var bytes = loadBundleIntoMemory(Path.Combine(Application.streamingAssetsPath, name));
    var bundle = AssetBundle.LoadFromMemory(bytes);

    Then I call LoadAllAssets<T>() for each type of bundle and store them in static properties that the game can use as a database. These are basically all ScriptableObjects that have refs to textures, sounds, sprites, etc that are used in the game.

    There are about 30 different types of assets that i load in this way during the startup.

    So what I am seeing during this load in the profiler is that for the first set of assets (not the bundle) to load, it is very slow. Then the about 10 will load in 0-40ms. Then another one is slow and the rest load in about 40ms. Would love to see if anyone has ideas on how i can solve this or what i am doing wrong.

    I'm trying to post a screenshot from the profiler but the forum is making this very hard.
     
    Last edited: Jul 22, 2019
  2. mikest

    mikest

    Joined:
    Feb 23, 2015
    Posts:
    29
    Here's my profiler Screen Shot 2019-07-22 at 1.04.01 PM.png
     
  3. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    332
    1. Why are you loading the bundle file into a byte array to load it vs using AssetBundle.LoadFromFile(...)?
    2. What compression option are you using to build these bundles (BuildAssetBundleOptions.UncompressedAssetBundle, BuildAssetBundleOptions.ChunkBasedCompression, BuildAssetBundleOptions.None)?
     
  4. mikest

    mikest

    Joined:
    Feb 23, 2015
    Posts:
    29
    Thanks for the reply

    I'm using LoadFromFile for standalone but the LoadFromMemory is the recommendation for a particular console. I could move this thread over to their forums but was curious what the overall best option was for asset bundles. I do not see this same issue with LoadFromFile (at least on standalone).

    These are currently set to no compression but I have tried all three options and they are roughly the same.
     
  5. mikest

    mikest

    Joined:
    Feb 23, 2015
    Posts:
    29
    ok, so it seems like if i call bundle.LoadAllAssetsAsync(); after loading the bundle, then the assets load more quickly in my LoadAllAssets call.

    basically, i would like to know why you think AwakeFromLoad is called 42,000 times in that profiler example above? There are about 600 assets in that bundle.
     
  6. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    332
    Ok, so I know which console you are using then, and yes, load from memory is correct as it removes the disk access limitations for that platform. Just make sure you build your bundles with BuildAssetBundleOptions.UncompressedAssetBundle as that is recommended for better patching on that console as well.

    LoadAllAssetsAsync & LoadAllAssets are basically the same API, just the Async one operates in a background, but once it is done all the assets are loaded, so calling LoadAllAssets again just basically returns as it is already done. In general though we recommend using the Async methods (LoadFromMemoryAsync, LoadAllAssetsAsync) as they prevent main thread hitches and keeps things running smoother. So you could pop up a loading animation / screen while this is going on easily.

    As for 'AwakeFromLoad is called 42,000 times' this is simply the number of Objects (not Assets) that are in the bundle. An Asset is a collection of Objects, so some Assets might only be made up of a single Object like Textures, others such as Prefabs or FBX files can be made up of 100s or easier 10000s of Objects depending on complexity. Though I am curious as to the types of Assets & Objects in this bundle and what version of Unity you are using.
     
    Last edited: Jul 22, 2019
  7. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    332
    Also another note is that in Project Settings, under Quality there are 3 Async Upload options that will provide a trade off of loading performance vs loading overhead that you might want to try changing (increasing) to see if that helps.
     
  8. mikest

    mikest

    Joined:
    Feb 23, 2015
    Posts:
    29
    Cool, thanks for info. All of this is currently behind a loading screen but it's up for way too long right now so trying to optimize what is being loaded and split it up between the title scene load and the first game scene load.

    I'm using Unity 2018.4.1f1 (LTS) but i recently updated from 2017.3 and the project was originally built in 5.3

    So based on what you said, i suspect there are primarily two things in this bundle causing the issue:
    about 200 large sliced sprite sheets (112 sprites per sheet)
    Screen Shot 2019-07-22 at 4.05.56 PM.png
    About 30-40 Tile map prefabs from the Tiled2Unity asset that have a lot of gameobjects with meshes
    Screen Shot 2019-07-22 at 4.05.08 PM.png
     
    Last edited: Jul 22, 2019
  9. mikest

    mikest

    Joined:
    Feb 23, 2015
    Posts:
    29
    I found an article on the Unity site that goes over those async settings so looking that over now. Thanks for the tip there.