Search Unity

Are Load calls synchronous if the asset is already loaded?

Discussion in 'Addressables' started by MoonstopSoftware, Aug 15, 2019.

  1. MoonstopSoftware

    MoonstopSoftware

    Joined:
    Jul 9, 2012
    Posts:
    4
    Hi,

    Does Addressables.LoadAsset run synchronously if the asset has previously been loaded?

    Thanks
     
  2. CharBodman

    CharBodman

    Joined:
    Sep 20, 2018
    Posts:
    36
    No. Addressables.LoadAsset has been depreciated/renamed.

    Addressables.LoadAssetAsync will always return a AsyncOperationHandle. You should never assume something has been previously loaded.
    You need to manage this yourself by caching the AsyncOperationHandle or result. This is best practice anyways because you may want to free those assets later.
     
  3. MoonstopSoftware

    MoonstopSoftware

    Joined:
    Jul 9, 2012
    Posts:
    4
    I see, thank you for the explanation.

    With that said, is there a way to Addressables.LoadAssetAsync all registered addressable, without providing keys? (So I can cache the result for synchronous lookup)

    Also, I assume due to the reference counter, that is it only when the counter is 0 that the asset is either loaded / unloaded. Otherwise an internal reference would be returned? (So there would be no performance hit to hitting LoadAssetAsync potentially many times in many places)

    Thanks again.

    Update - It looks like labels are the way to go for this.
     
    Last edited: Aug 15, 2019
  4. CharBodman

    CharBodman

    Joined:
    Sep 20, 2018
    Posts:
    36
    Hmm Yeah, I assume there is not performance hit with multiple Loads. These should be cached from my understanding. Correct only when references are 0 will the asset be released.

    Yeah labels would be the way to go. Otherwise you can load via directory.
     
  5. MoonstopSoftware

    MoonstopSoftware

    Joined:
    Jul 9, 2012
    Posts:
    4
    I wrote the code below to await upon, with the intention this will pick up every Addressable. However, this also appears to include duplicate of my Addressables with PrimaryIds of: "UI/Alert.png" and "UI/Alert.png.Alert".

    Code (CSharp):
    1.     public async Task AsyncLoadAssets() {
    2.         await Addressables.InitializationOperation.Task;
    3.  
    4.         foreach(var locator in Addressables.ResourceLocators) {
    5.             var handle = Addressables.LoadResourceLocationsAsync(new List<object>(locator.Keys), Addressables.MergeMode.Union, typeof(Object));
    6.  
    7.             await handle.Task;
    8.  
    9.             foreach(var location in handle.Result) {
    10.                 Debug.Log(location.PrimaryKey); //There seems to be duplicates in here?
    11.                 //PERFORM ASYNC LOADING LOGIC HERE & STORE CACHE
    12.             }
    13.         }
    14.     }