Changing Sprite Library Asset in script

Is there any way to change the Sprite Library Asset in a script? Basically trying to use the same animations but with different sprites, depending on what the user chose in the previous scene.

2 Likes

If the animation does not actually “drive” the value of the Sprite field in the SpriteRenderer, you can always just use a reference to the SpriteRenderer and change the Sprite (or any other non-driven properties) however you like. The animation shouldn’t care in that case.

If the animation actually “drives” the value of the Sprite field in the SpriteRenderer, then you cannot do this without changing the animation to NOT drive the Sprite field.

@linusfackler are you asking if you can change the content of a Sprite Library Asset or the Sprite Library Asset being referenced by a Sprite Library component?

If you would like to change the content of a Sprite Library Asset in runtime, you can either create overrides in the Sprite Library component (link to docs) or change the Sprite Library Asset directly (link to docs).

If you would like to change the Sprite Library Asset being referenced by a Sprite Library component, you can set it using the public property spriteLibrary.spriteLibraryAsset (link to docs).

Let us know how it goes!

1 Like

I find this API to be frustrating and confusing.

There is a hidden set of information somewhere within Unity regarding the SpriteLibraryAsset. The Sprite Library Editor can appear to be empty even if the Sprite Library Asset has a bunch of Sprites in it. These Sprites can be seen when you add the asset to the component.

I’m sorry to hear that the API is confusing. Do expand on it, if you like, so that we have a bit more info to work on when taking it into consideration.

This sounds like a bug. Would you mind filing a bug report, so that the team can take a closer look?

Thanks!

Thanks for the response. Hopefully, I can explain this clearly. The water feels very muddy.

For one, this UI has basic issues. A quick improvement would be to at least have a hover-over tooltip that displayed the Sprite’s full name. And images should never be skewed. How did this ship to customers like this?
9244578--1292172--upload_2023-8-23_7-36-31.png

Why does each and every Sprite Renderer component that has a Sprite Resolver also need a Sprite Library component? It seems to create a lot of redundancy that is unnecessarily more complicated. It really just needs a way to manage overrides I imagine.

I was told previously that

spriteLibraryComponent.spriteLibraryAsset.AddCategoryLabel(sprite, category, label);

does not actually save to the underlying Sprite Library Asset (which is really confusing). You actually mention it yourself here: https://discussions.unity.com/t/855093/30

Just to be clear when I say “Sprite Library Editor” is empty I am referring to this editor. Not the Sprite Library component inspector editing UI mentioned above.

9244578--1292202--upload_2023-8-23_8-9-33.png

If saving to the Sprite Library Asset via script didn’t save to the Sprite Library Source Asset were completely true I would imagine that when I delete the Sprite Library component and re-add a component it would be empty even after I reassign the Sprite Library Asset field with an empty .spriteLib asset. But when I reassign the Sprite Library Asset field all the sprites reappear even though the Sprite Library editor is empty.

This is all really confusing, broken, and caused by underlying design issues in the API.

AddCategoryLabel should save to the source asset. Otherwise, why even have a separate AddOverride method?

Hope this helps.

Great, thanks for diving deeper into the feedback. I no longer work in this area at Unity, but I’ll pass the feedback along internally.

1 Like