user10577558
user10577558

Reputation:

Unity Loading Sprites with Addressables

I am very new to Unity and after doing some research I found a lot of discussion on how I shouldn't use Resources.Load and instead use Addressables.

Previously I was loading card art with

cardPrefab.cardArt.sprite = Resources.Load<Sprite>("CardSprite/Justice");

However I can't seem to get Addressable to work. Trying the following gives me an error:

 Sprite Test = Addressables.LoadAssetAsync<Sprite>("CardSprite_Justice");

I get this error:

Cannot implicitly convert type 'UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<UnityEngine.Sprite>' to 'UnityEngine.Sprite'

Which is really confusing.

Upvotes: 0

Views: 8486

Answers (2)

Arshad Ali
Arshad Ali

Reputation: 11

void Start()
{     
       Addressables.LoadResourceLocationsAsync("SpriteName",typeof(Sprite)).Completed += SpriteLocation;

}

 void SpriteLocation(AsyncOperationHandle<IResourceLocation> obj)
    {  
      Addressables.LoadAssetAsync<Sprite>(_spriteName).Completed += obj => item.mysprite.sprite = obj.Result;

   }

Upvotes: 0

TimChang
TimChang

Reputation: 2417

Get it with callback

private void Sprite_Completed(AsyncOperationHandle<Sprite> handle)
{
    if (handle.Status == AsyncOperationStatus.Succeeded)
    {
        Sprite result = handle.Result;
        // Sprite ready for use
    }
}

void Start()
{
    AsyncOperationHandle<Sprite> SpriteHandle = Addressables.LoadAsset<Sprite>("CardSprite_Justice");
    SpriteHandle.Completed += Sprite_Completed;
}

and you can use coroutines or task to get it , watch document : https://docs.unity3d.com/Packages/[email protected]/manual/AddressableAssetsAsyncOperationHandle.html

Upvotes: 2

Related Questions