Search Unity

addressables and sprite atlas memory best workflow

Discussion in 'Addressables' started by cemleme, Oct 18, 2019.

  1. cemleme

    cemleme

    Joined:
    Mar 24, 2017
    Posts:
    30
    Hello,

    I am planning ahead for using my sprite atlases with addressables and would like to ask what would be best memory wise.

    Let's say the player has an inventory, there are 1000 items in the game, 10 of them in inventory and there is a sprite atlas keeping all the possible item icons.

    Should I preload this sprite atlas using addressables at the beginning cache it into a variable until the game is closed

    or should I load this sprite atlas when inventory opens, get the required sprites from it and then unload the sprite atlas?

    keeping the sprite atlas keeps 1000 items in the memory. Isn't it better to keep those sprites separately? Should I consider increasing the draw calls or is it a no, no matter what?

    Thanks
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    You may want to share details about the size of items, and how predictable the drop of inventory is.

    A common practice is limit the scope of inventory drops and preload things needed.

    Draw call is an unrelated thing, usually draw sprites on the same atlas share the same material will get batched.
     
  3. cemleme

    cemleme

    Joined:
    Mar 24, 2017
    Posts:
    30
    Hi thanks for the reply!

    Let me clear it up. Let's ignore the draw call part as it was kind of unnecessary

    When I open the UI Canvas, I check all the items in my inventory and load the sprite from sprite atlas in addressables

    I load the item sprite like this:

    Code (CSharp):
    1.     Addressables.LoadAssetAsync<Sprite>("InventoryIcons[" + item.label + "]").Completed += OnIconLoaded;
    2.  
    3.     private void OnIconLoaded(AsyncOperationHandle<Sprite> obj)
    4.     {
    5.         itemImage.sprite = obj.Result;
    6.         Addressables.Release(obj);
    7.     }
    question 1: should I use Addressables.Release on the handle everytime I load the sprite? If I don't do that icons of different image slots get mingled with each other.

    question 2: should I preload this sprite whenever a new item is picked up? In that case I shouldn't release the handle right? how do I preload it?

    should I use
    Addressables.DownloadDependenciesAsync
    or
    Addressables.LoadAssetAsync<Sprite> as regular call which caches the asset? and then don't release the handle? (and when I don't release it different calls return wrong sprites?)

    note: I know loading simple sprites might not effect the memory much but I just want to do it the right way.

    Thanks
     
  4. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    q1. No need to release handle manually. The system will do that for you, when your asset ref count reach 0. Image slots get mingled, could be a bug or something else, you may want to start another bug report thread to discuss.

    q2. As I said, "A common practice is limit the scope of inventory drops and preload things needed." If you can predict the range of items in a level, you can preload all of them before starting a game to avoid the allocation spike.

    q3. When I say preload, I mean load the asset into memory by LoadAssetAsync, and LoadAssetAsync will download dependencies as well. Depends on your game type, web game may download asset while playing, mobile may download everything before start a game.

    Again, the return wrong sprite thing seems a bug.
     
    hasanerzi likes this.
  5. cemleme

    cemleme

    Joined:
    Mar 24, 2017
    Posts:
    30
    thanks a lot for your time.

    just a quick very basic question.

    what is the proper way to preload.

    should I preload the dependency or should I LoadAssetSync everything I want to preload.

    do you have a suggestion of a correct way of keeping preloaded assets? something like singleton dictionary?
     
  6. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    Use DownloadDependencies api to prefetch bundles. And use LoadAssetsAsync api to load into memory and perhaps stores into a singleton.