- Home /
Addressables and Sprites: Why does this work, but this does not?
public static void LoadSprite(SpriteRenderer renderer, string path)
{
AsyncOperationHandle<Sprite> handle = Addressables.LoadAssetAsync<Sprite>(path);
handle.Completed += (AsyncOperationHandle<Sprite> obj) =>
{
if(obj.Status == AsyncOperationStatus.Succeeded)
{
renderer.sprite = obj.Result;
Addressables.Release(obj);
}
else
{
Debug.Log("Sprite Load Failed");
}
};
}
public static void LoadSprite(Sprite sprite, string path)
{
AsyncOperationHandle<Sprite> handle = Addressables.LoadAssetAsync<Sprite>(path);
handle.Completed += (AsyncOperationHandle<Sprite> obj) =>
{
if(obj.Status == AsyncOperationStatus.Succeeded)
{
Debug.Log(obj.Result.name);
sprite = obj.Result;
Addressables.Release(obj);
}
else
{
Debug.Log("Sprite Load Failed");
}
};
}
I am trying to use Addressables so that I can load sprites from a json file. Above are two ways I have tested for doing this; LoadSprite(mySpriteRenderer, filePath) successfully changes the sprite of a game object in my scene when I pass it the SpriteRenderer that was attached. But I would prefer to do it the second way, because I have a class which I want to store a Sprite, but does not have or need a SpriteRenderer to it. If anyone knows what explanation there is for why LoadSprite(mySpriteRenderer.sprite, filePath) would not work I might be able to figure out what's wrong or if it's at all possible to do it the way I want. The code reaches Debug.Log(obj.Result.name) in the second case and prints the name of the sprite I expected, and the sprite I am loading in both cases comes from a sprite sheet (not a sprite atlas) if that is any clue as why the second case fails. Thanks in advance whoever responds.