Search Unity

How to get the addressable resource context with version 1.1.7 ?

Discussion in 'Addressables' started by FredericID, Aug 12, 2019.

  1. FredericID

    FredericID

    Joined:
    Feb 18, 2019
    Posts:
    1
    Hi,

    Previously I have been using the Addressables in version 0.5.3 and I was able to get the addressable resource context like in the following code exemple (using op.Context to get the ID Grand_Place) :

    yield return Addressables.LoadAssets<Sprite>("sprites_all", op =>
    {
    //op.Context = Assets/Data/Sprites/Grand_Place/1_SpriteName.jpg

    string locationID = op.Context.ToString().Split('/')[3];

    ...
    });

    But now with the version 1.1.7, I can't do it anymore. The option "Context" is not available. So my question is how can I get the addressable resource context with the new following code :

    AsyncOperationHandle<Sprite> request = Addressables.LoadAssetAsync<Sprite>("sprites_all");

    yield return request;

    if(request.Status == AsyncOperationStatus.Succeeded)
    {
    //Need to get the full resource context here to be able to do exactly the same as in the previous code

    request. ?

    ...
    }

    Thank you in advance for your help
     
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    We got rid of that for two reasons. For one, it wasn't actually right all that much of the time. Second was we cleaned up our loading (specifically reference counting) making it more robust, and in the process made it less likely for there to be a "context" that made sense all the time.

    So the way you get what you want is to do this...


    var handle = Addressables.LoadResourceLocationsAsync("sprites_all");
    yield return handle;


    Now you have a list of IResourceLocations. Each has an InternalId that will tell you what Context used to. Depending on what you were planning to do with that context you can either load only the ones you want, or load them all in a loop, and put them in some structure that associates InternalId with asset.
     
  3. zyc_dc

    zyc_dc

    Joined:
    May 11, 2018
    Posts:
    42
    @unity_bill
    Seems that load in a loop is much slower than Addressables.LoadAssets when load many small files. Perhaps it is caused by that yield return op need at least one frame to execute? Is there any walk-around?
     
    Last edited: Sep 18, 2019
  4. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    970
    If you are doing Load then yield in a loop, then yes, that would be why it is slow, as it will take at least as many frames as you have items. You can fire off all the load asset operations storing the AsyncOperation in a list, and then yield while any operation in the list is not complete, rather than yielding for each operation individually.
     
    RecursiveFrog likes this.
  5. zyc_dc

    zyc_dc

    Joined:
    May 11, 2018
    Posts:
    42
    Yeah. You're right. Now I use await instead of coroutine. It works now