Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help - Getting a sprite's atlas during build

Discussion in '2D' started by Setsuki, Oct 31, 2018.

  1. Setsuki

    Setsuki

    Joined:
    Aug 21, 2012
    Posts:
    31
    Hello,
    I am currently using the same texture for multiple sprites, which have different PPUs.
    However, this means that in my build, I have several duplicates of the same texture, or multiple instances of the same texture in the generated SpriteAtlas.

    So I made a ScriptableObject ("Variants") that contains Sprites as subassets, which are variants of an existing sprite ("Original").
    Sans titre.png
    The goal is to have multiple sprites pointing at a same texture.

    However, I encounter a problem when using SpriteAtlases, as I need to rebuild the variants when the "Original" sprite is packed:

    • When entering play mode the original sprite is automatically changed to use the spriteAtlas instead of the original texture. So when entering play mode, my variants regenerate themselves in order to use the spriteAtlas as well.
    • When quitting play mode, my variants go back to the original texture.
    • When building, the variants should use the spriteAtlas again. However, my variants do not regenerate, and are still using the original texture.

    How could I make it so that the variants use the atlas instead of the original texture when building?

    My guess is that I would need a callback that happens after the sprites are packed, but before the resources are built.

    Thank you
     
    Last edited: Nov 9, 2018
    aurelien-morel-ubiant likes this.
  2. aurelien-morel-ubiant

    aurelien-morel-ubiant

    Joined:
    Sep 27, 2017
    Posts:
    275
    This method is something that we tried to do in our company (with the guy right above).

    It could be really interesting if someone at Unity have an answer concerning this !

    @MelvMay do you know if it's something that accessible during build or is it something missing in the engine ? Or maybe anyone else at Unity could answer to this if it's maybe related to 2D logic.

    This topic is related to this one cause it's another feature that could be usefull :
    https://forum.unity.com/threads/sug...-multiple-pixels-per-unit-per-texture.564157/

    But it's a different topic that could be discuss in its own topic.

    Thanks in advance cause it's something that could be usefull to avoid multiple sprite duplication for almost nothing.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Sorry, I work on 2D physics. I can ask one of the 2D team to reply though.
     
    aurelien-morel-ubiant likes this.
  4. Setsuki

    Setsuki

    Joined:
    Aug 21, 2012
    Posts:
    31
    Oh, sorry. Well, if that's not too much to ask, could you, please?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    I did ask for you, they should reply soon hopefully.
     
  6. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    638
    @Setsuki

    As mentioned in manual (https://docs.unity3d.com/Manual/class-SpriteAtlas.html) :
    User will be able to create another Sprite Atlas asset and declare it is a variant of an existing Sprite Atlas in the project. It will then duplicate the Master’s atlas texture and resize it according to a multiplier.

    When you enter playmode or build, for each variant atlas the packing system creates copies of textures in the corresponding master atlas and scales down based on Multiplier value.

    This is precisely what Variant atlas. Create copies of Atlas Textures from the Master SpriteAtlas and scale them down. Also Atlas textures are only active when in Playmode/Build. In Editor mode, Sprites always use Original textures.

    If there are any changes in the Master Atlas, the Variant Atlas Textures are updated as well. If this is not the case, please file a bug report with a simple repro project and we will take a look asap.

    Thanks.
     
  7. Setsuki

    Setsuki

    Joined:
    Aug 21, 2012
    Posts:
    31
    Hello @Venkify, and thank you for your answer!

    However, unless I haven't understood your answer, the Sprite Atlas variants don't fit the problem I am trying to fix.
    Yes, they allow to have different scales for the atlases, but still can't have multiple sprites with different Pixel Per Units using the same texture.

    I had posted a GIF in another thread that should illustrate the problem I'm trying to solve

    giphy.gif
    Here, the Image component changes its pixel Per Units based calculations.


    I am trying to find a way to have this directly in sprites, instead of requiring to have each component overriding the sprite's original PPU ; This is the image I posted in the original post, which doesn't work as soon as Sprite Atlases are involved.

    In case I'm not making sense, not being clear enough (I'll admit, I have a hard time explaining this one), you can find an example project of the intended goal, which should render this
    Annotation.png
     

    Attached Files:

    aurelien-morel-ubiant likes this.
  8. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    638
    @Setsuki So based on the test project :

    1) Import a Sprite 1 (from Source Texture A)
    2) Create a few sprites 2, 3, 4 with different PPU from the same Source Texture A.
    3) Add them to same Atlas.

    PixelPerUnits is a TextureImporter property and Sprite 1 generated from the TextureImporter from Source Texture A should behave normally with Sprite Atlas. However Sprites 2, 3, 4 do not share the property from TextureImporter and are dynamic in nature. We will look into adding support for such use cases (SpriteAtlas support for varied PPU, dynamic sprites etc) in the future.

    However I would suggest a work-around to using 2 Sprite Atlases. Sprite Atlas SAA for Sprite 1 and Sprite Atlas SAB for the created sprites 2, 3, 4. Let me know if this works. Please do update if anything is unclear. Would also be great if you can add the exact use-case in the Project file. I can only find a scene with some dynamic sprites used in Image components and could not find any Atlas being used at all.
     
    aurelien-morel-ubiant likes this.
  9. Setsuki

    Setsuki

    Joined:
    Aug 21, 2012
    Posts:
    31
    Hello @Venkify, thank you for taking the time to try the project out!
    I'm sorry that I did not include sprite atlases for this sample project, I basically... Forgot.

    First of all, looking into adding support for this is great news, I hope this happens. Do you need me to create a ticket on Unity Feedback ?

    After trying your suggestion, I was surprised to see that my generated sprites were compatible with SpriteAtlases. It... Does work, but there is still the same problem :
    Annotation.png
    The left Sprite Atlas, SAA (left) packs sprite 1, and SAB (right) packs sprites 2, 3 and 4.
    As you can see from the preview, the texture is still packed 4 times.

    I've added folders with different sprite atlases, including your suggestion, to show how I, as a user, would expect "Sprite Variants" to work.

    Thank you for your time.
     

    Attached Files:

    aurelien-morel-ubiant likes this.