Access texture generated from SpriteAtlas

How do I access a Texture generated from SpriteAtlas asset? The asset can preview the entire texture in editor so clearly the editor has access to it.

@rustum would you please assist in this situation? You deprecated the old packer, where this was possible, is this possible in new SpiteAtlas system? Even if there is no API for it, is not there a way how to “hack” and get a texture from it?

1 Like

Hi @Wrymnn . Thanks for reaching out!
Could we get a bit more information on what you are trying to achieve?
@Venkify can help to clarify more as well.

@rustum thank you for the reply!

We require custom UI particle system, that is using NGUI`s MaskableGraphics component. This component requires Texture2D to render, however.

Unfortunately unity`s particle system is not sufficient, nor are UI extensions for it, nor are other packages from asset store, nor we can use multi-sprite (due to workflow restrictions).

This all would be possible with the old system (quite easily actually), but since it’s deprecated and packing tags can no longer be used with new Unity versions it seems, we cannot really get the texture unity generates as atlas from different sprites.

The Texture2D used by Sprites can be accessed through https://docs.unity3d.com/ScriptReference/Sprite-texture.html
Could you please clarify on how you are accessing it ?

@Venkify I have already tried that. Sprite.texture returns the texture of that single sprite, not that of SpriteAtlas. It would not even work since a single Sprite can be packed into many SpriteAtlases and Sprite.texture can point only to a single texture.

p.s. Sprite.texture worked with deprecated system, where a sprite could have a single packing tag and unity automatically packed those sprites into atlases.

Packing a Sprite into a single SpriteAtlas would result in the same result as using packing Tag and you should be able to access the Atlas texture as before. Could you please be more specific on what you are trying to achieve ?

Now I know where the issue is. Yes it works, but only in Play mode. We need it to work in Editor mode as well.

Unity should already have access to this atlas texture in editor mode, since it can preview the texture in the inspector, in Edit mode.

We need to have a custom UI particle system, that is using MaskableGraphics component, so it get`s rendered in Canvas. We need this particle system to work also in editor mode, so our artists can edit and iterate it.
Since particles can use different sprites, we need to pack them into SpriteAtlas, and assign the generated Texture to MaskableGraphics and use the sprite UVs to generate the mesh.

@Venkify any news on this? Is the generated texture possible to access from SpriteAtlas in editor mode? It was possible with the deprecated packer system.

Its not possible to use the Texture from SpriteAtlas directly in Editor mode. This is because the Atlas Texture may change anytime if 1) Any change made to SpriteAtlas, 2) Reimport Project, 3) Change Platform etc… as the Textures are packed again and hence its not safe.

1 Like

so for example, if i want to spawn a VFX particle that shows a certain item icon, and said icon is inside a sprite atlas, how do we proceed to do that?

Find a way to Invoke

SpriteAtlasExtensions.GetPreviewTextures

see:

You can get the preview texture like this

var method = typeof(SpriteAtlasExtensions).GetMethod("GetPreviewTextures", BindingFlags.Static | BindingFlags.NonPublic);
object obj = method.Invoke(null, new object[] { spriteAtlas });
Texture2D[] textures = obj as Texture2D[];

Now, I need to find a method to get the coordinates for each sprite from the preview texture.

5 Likes

There are methods to get texture & sprite uv using SpriteUtility

SpriteUtility.GetSpriteTexture
SpriteUtility.GetSpriteUVs

2 Likes