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

Bug LoadAssetsAsync CANNOT Unpack SpriteSheet (Workaround found)

Discussion in 'Addressables' started by Reimirno7, Apr 17, 2022.

  1. Reimirno7

    Reimirno7

    Joined:
    Nov 25, 2021
    Posts:
    51
    Hi, So normally,

    LoadAssetAsync<Texture2D>("YourSpriteSheetName")
    allows to load that image as a Texture2D, but you have no access to the individual sprites you sliced.
    LoadAssetAsync<IList<Sprite>>("YourSpriteSheetName")
    allows to load a list of sprites directly from an texture2D that has multiple sprites as subobjects. It automatically unpacks all sprites for you.

    Problem:
    But
    LoadAssetsAsync<IList<Sprite>>("LabelSharedByAllSpriteSheets", callback)
    does not work.

    The scenarios is that I have 30+ sprite sheets sharing the same label and I want to load all of them into memory at the same time. I notice that
    LoadAssetAsync<Texture2D>("LabelSharedByAllSpriteSheets", callback)
    still works but it is not useful as I cannot access the sliced sprites.

    To be specific:
    Code (CSharp):
    1.       Addressables.LoadAssetsAsync<IList<Sprite>>("LabelSharedByAllSpriteSheets", singleRlt =>
    2.       {
    3.         var sprites = singleRlt.ToList(); //and then dosomething with the List of sprites
    4.       });
    gives you a Invalid Key Exception.

    My workaround:
    Load asset in two steps. First load their locations, and then load the actual sprite, so we can avoid
    LoadAssetsAsync
    .

    Code (CSharp):
    1. Addressables.LoadResourceLocationsAsync("LabelSharedByAllSpriteSheets").Completed += h=>
    2.       {
    3.         var allLocations = h.Result;
    4.         foreach (var x in allLocations)
    5.         {
    6.           Addressables.LoadAssetAsync<IList<Sprite>>(id).Completed += handle =>
    7.               {
    8.                 if (handle.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
    9.                 {
    10.                   var sprites = handle.Result.ToList();
    11.                   //do something with the list of sprites
    12.                 }
    13.                 Addressables.Release(handle);
    14.               };
    15.         }
    16.         Addressables.Release(h);
    17.       };
    Hope that helps.

    Also, can someone tell me whether
    LoadResourceLocationsAsync 
    is deprecated? It seems to be so in older versions of Addressable package but not any more in newer version? I am confused.
     
    Last edited: Apr 17, 2022
  2. zagrizzl

    zagrizzl

    Joined:
    Jul 17, 2019
    Posts:
    4
    Thank you for this! Seems like Unity should have done this behind the scenes.