Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Access texture generated from SpriteAtlas

Discussion in '2D' started by LukasFMA, Jan 4, 2021.

  1. LukasFMA

    LukasFMA

    Joined:
    Apr 21, 2016
    Posts:
    2
    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.
     
  2. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    @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?

    upload_2021-1-5_8-48-49.png
     
    Tony_Max likes this.
  3. rustum

    rustum

    Unity Technologies

    Joined:
    Feb 14, 2015
    Posts:
    190
    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.
     
  4. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    @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.
     
    Last edited: Jan 5, 2021
  5. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    618
  6. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    @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.
     
  7. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    618
    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 ?
     
  8. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    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.
     
  9. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    @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.
     
  10. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    618
    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.
     
    JadDeeb likes this.
  11. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    159
    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?
     
  12. oOtroyOo

    oOtroyOo

    Joined:
    Jun 7, 2015
    Posts:
    4
  13. Plapi100

    Plapi100

    Joined:
    Mar 15, 2014
    Posts:
    3
    You can get the preview texture like this

    Code (CSharp):
    1. var method = typeof(SpriteAtlasExtensions).GetMethod("GetPreviewTextures", BindingFlags.Static | BindingFlags.NonPublic);
    2. object obj = method.Invoke(null, new object[] { spriteAtlas });
    3. Texture2D[] textures = obj as Texture2D[];
    Now, I need to find a method to get the coordinates for each sprite from the preview texture.
     
    NMJ_GD likes this.
  14. Plapi100

    Plapi100

    Joined:
    Mar 15, 2014
    Posts:
    3
    GregKorneu likes this.