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

How to check if AssetReference already loaded?

Discussion in 'Addressables' started by wiganda, Aug 5, 2021.

  1. wiganda

    wiganda

    Joined:
    Feb 24, 2018
    Posts:
    14
    Pretty new to addressables and think I'm missing something.

    I use a simple coroutine (as below) to load any AssetReferenced sprite into any SpriteRenderer.

    Code (CSharp):
    1. public void SetSprite(SpriteRenderer _spriteRenderer, AssetReference _sprite)
    2.     {
    3.         StartCoroutine(SetSpriteRef(_spriteRenderer,_sprite));
    4.     }
    5.  
    6.     private IEnumerator SetSpriteRef(SpriteRenderer _spriteRenderer, AssetReference _sprite)
    7.     {
    8.        var _currentSpriteOperationHandle = Addressables.LoadAssetAsync<Sprite>(_sprite);
    9.         yield return _currentSpriteOperationHandle;
    10.         _spriteRenderer.sprite = (Sprite)_currentSpriteOperationHandle.Result;
    11.     }
    Then to load a particular AssetReference into a particular SpriteRenderer I do:

    Code (CSharp):
    1. SetSprite(spriteRendererName, spriteAssetReference);
    But how, at a later time, can I find out if an AssetReference has already been loaded, so I do not needlessly run the SetSprite coroutine again for that same AssetReference (whether I want to load it into the same or another SpriteRenderer)?
     
  2. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    70
    I'm not sure, but I don't think there's a way to ask Addressables whether a specific asset is is loaded or not. I think you'll have to keep track of that yourself.
     
  3. wiganda

    wiganda

    Joined:
    Feb 24, 2018
    Posts:
    14
    Appreciate the reply.
    I did find some info and now currently use both .IsValid() and .IsDone on a handle's specific result (that I might want to assign to a spriteRenderer) to first check if its already loaded, if so, then assign the existing Result instead of loading. But of course this unforuntately doesn't work with the way I loaded assets in the code above as there's no way to keep track of handles that way, so I had to change how I load addressables by loading whole groups, each with their own handle and results.