Help - Getting a sprite's atlas during build

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”).
3841417--324487--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

1 Like

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 :

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.

Sorry, I work on 2D physics. I can ask one of the 2D team to reply though.

1 Like

Oh, sorry. Well, if that’s not too much to ask, could you, please?

I did ask for you, they should reply soon hopefully.

@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.

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

3889090--330688--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
3889090--330703--Annotation.png

3889090–330697–MultiPPU Example.zip (52.4 KB)

1 Like

@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.

1 Like

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 :


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.

3892459–331072–MultiPPU Example.zip (58.6 KB)

1 Like