3
\$\begingroup\$

I've been dabbling with Addressables for a while, and overall everything works fine with AssetReferenceT - I can load Textures and just about anything else, but for some reason I cannot seem to get to load Sprite or Texture2D.

Given an AssetReferenceT<Sprite>, or AssetReferenceSprite, assigned to a Single Sprite that is addressable...

var icon = await reference.LoadAssetAsync<Sprite>();

And similar variations will just give me some form of:

Exception: Unable to load asset of type UnityEngine.Sprite from location Assets/triangle 1.png[09adbfd8aff3bbd43890e8aabd7f13ec].

I'm rather confused as this is such a simple thing to do - I already do it with all other asset types but I just can't make it work for Sprite and Texture2D. The closest I was able to achieve was using AssetReferenceT<Texture> and then creating the Sprite at runtime, but that doesn't seem ideal.

Addressables 2.1.0, Unity 6000.0.9f1 (but I've been running into this for a long time).

The reference is set without warnings:

enter image description here

enter image description here

\$\endgroup\$

1 Answer 1

2
+50
\$\begingroup\$

[This answer was written using Unity 6000.0.10f1 with the packages "Addressables 2.1.0" and "2D Sprite 1.0.0"]

When you import a .png file as a "Sprite (2D and UI)", then it will actually become an asset with a sub-asset for the sprite. That's because using "Sprite Mode: Multiple" you can potentially slice a single .png file into multiple sprites. But for sake of uniformity, "Sprite Mode: Single" are treated the same way. They get a single "Sprite" sub-asset:

texture with sprite

When you have an AssetReference to such a "complex" asset file, then it will offer you to pick a sub-asset. Which is why your inspector screenshot shows you not one but two slots side-by-side in your asset reference. The first is the texture, and the second is the sprite in the texture. When you assign such an asset, then the editor will automatically pick that first (and in this case only) sub-asset for you.

However, that's actually wrong in this case. You need to make it point to the "<none>" sub-asset:

Asset Reference

It will then load that one sprite asset in that texture asset correctly:

result

It appears that asset references to sprites don't actually support sub-sprites (yet?). I tried to turn my sprite into a 2x2 tileset and reference one of the sub-sprites. It didn't work and failed with the same error you received. Loading the "<none>" sub-asset loaded the first sub-sprite.

However, you can reference sub-sprites when you foregoe AssetReferences. Simply reference assets by their addressable strings and load them via Addressables.LoadAssetAsync<Sprite>(assetPath). In that case, you have to write the asset path, followed by the name of the sub-asset in square brackets. Like this:

string asset path

Here is the result. The upper sprite refers to the asset with the string MySprite[MySprite_3], the lower one with an AssetReference of "MySprite" and "<none>".

result

Curiously, you actually get the same behavior with the specialized class AssetReferenceSprite. Why did Unity Technologies create this specialized asset reference with a custom inspector that allows you to pick sub-sprites but actually doesn't support sub-sprites? You would have to ask the developers that question.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .