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. Dismiss Notice

How to load an entire addressable asset bundle into memory?

Discussion in 'Addressables' started by Diorgo24BitGames, Feb 21, 2020.

  1. Diorgo24BitGames

    Diorgo24BitGames

    Joined:
    Sep 13, 2018
    Posts:
    5
    Hi

    How do you load an entire addressable asset bundle into memory?
    So that you can load prefabs from the bundle (in memory) to reduce disk access.

    This is especially useful to speed up loading.

    In the old asset bundle system you could use AssetBundle.LoadFromMemory or AssetBundle.LoadFromMemoryAsync
    But there doesn't seem to be similar methods for addressable bundles.


    .
     
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    566
    I believe Addressables handles all of that for you. When you load an asset, it increments an internal reference counter. When you unload the same asset, it decrements that internal reference counter. When the counter reaches 0, then it unloads it from memory. If you want it to always remain in memory, just don't call unload.

    Personally, I use my own memory cache so I can precisely control how all that happens, but it shouldn't be necessary for your use.
     
  3. Diorgo24BitGames

    Diorgo24BitGames

    Joined:
    Sep 13, 2018
    Posts:
    5
    @ProtoTerminator thanks for the feedback.

    We're hoping to find a solution that loads the entire bundle into memory, then we load the prefabs from the bundle. The goal being to have only one disk read instead of multiple disk reads (i.e. avoid a disk read per prefab).

    .
     
  4. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    566
    Then it already works as you want. It will remain in memory until you fully release it.
     
  5. magmagma

    magmagma

    Joined:
    Oct 27, 2017
    Posts:
    41
    I believe what will happen is that when one object is loaded, that object will remain in memory until you release it, but if you have an asset bundle with 100 different objects, only the one you just loaded will now be in memory.
    I.e. without further information, you would need to manually load each and every one of the objects in the bundle to have them in memory.

    And I believe he is asking if there is something already available that takes a whole bundle and automatically loads everything inside so that it is ready to use in RAM instead of having to do it manually.

    I don't believe there is such thing but I might be wrong, as I haven't had to do that myself until now so I haven't looked much into it so far. Also, somebody else might have done it already, hopefully someone more knowledgeable in this particular topic can answer.
     
  6. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    I don't think Addressables supports the LoadFromMemory workflow, but I believe you can select the bundle compression method. When you use LZMA the bundle will be fully loaded into memory because LZMA doesn't support streaming assets directly from the bundle file.

    Keep in mind there are two downsides to this:
    • The bundle decompression might take a while (I think it's single threaded)
    • It will double your memory usage: the bundle in memory is still considered a "file", so when you load an asset from it the asset data will still be copied elsewhere in RAM before it can be used.
     
  7. Diorgo24BitGames

    Diorgo24BitGames

    Joined:
    Sep 13, 2018
    Posts:
    5
    @KokkuHub thanks for the info. We'll try that out.

    .
     
  8. Krnitheesh16

    Krnitheesh16

    Joined:
    Apr 22, 2020
    Posts:
    13
    You can use Asset Labels