Creating sprite atlas at runtime?

Our game will have a lot of sprites that will be used across different scenes. Let’s say we will have 500 sprites in total, and each scene will use 50 of them, but most sprites will be used in several scenes.

So having all the sprites in a single sprite atlas is not a good idea, because it would be too big.
Having a sprite atlas for each scene will be a waste of space, because the same sprite will be in several atlases.
And not using atlas at all will increase draw calls and affect performance.

The perfect solution will be generating a new atlas at run time when a new scene is loaded, with the sprites that will be used. Is this possible with Unity?

1 Like

I found this, and it may work: GitHub - DaVikingCode/UnityRuntimeSpriteSheetsGenerator: Unity – generate SpriteSheets at runtime!

But created textures are so big in memory

Hi, I’m also interested with generating sprite atlas at runtime, i’ve been looking at the link you sent.
Did you try it ? I’m interrested to have some feeback about this lib :slight_smile:

I’m a bit worried with the size of the texture, since, correct me if i’m wrong, it’s putting every asset according to there bounding box, instead of the “tight” version of unity atlas.
But even if there was a “tight” filling it wouldn’t be great to use it at runtime, it would take a lot of time to generate, so it’s not an option.

I’ve been thinking of another option: auto generate sprite atlas for unity editor, all your assets would have an information about there location in the game, you make a python script or else, that creates all the atlas you will need. it won’t use memory, an atlas just takes the reference of the images, it will be long to generate in unity everytime you have a change, but I think it’s viable.

I abandoned the idea to create texture atlas at runtime: the approaches I tried were slow and didn’t work well. Anyway, if you find a reliable way to do it, please let us know.

Oh ok, can your share your approach ? I’m gonna tell you about mine, maybe you will see a problem in it that can save me times :slight_smile:

For the context: it’s a 2D game with parallaxe, there’s several levels in wich there’s several assets that I want to put in atlas to limit draw calls.

I want to have a main atlas that has every asset I need for the current background, this one will be created at initialisation.
And, asynchronously generate the atlas for the next level.
when we go the next level we swap the atlasses, the generated one becomes the main, and the main becomes the new buffer for the next atlas etc…

Note: when i say “Atlas” it can be several, if my texture is more than 8192 x 8192 it will create another atlas, and everything will be stored depending on the depth on the screen (to optimise draw call)

The approach I tried is the link you saw in my post: GitHub - DaVikingCode/UnityRuntimeSpriteSheetsGenerator: Unity – generate SpriteSheets at runtime!

Instead of using dynamic texture atlas, I think it’s better to use Unity as it’s supposed to… you will have less problems. You can use Unity built-in tile system for the backgrounds, so you will save memory.

Here you go

Lol I just asked myself this question and then the dude answered in a “dead” topic

Don’t necro threads please. Especially with threads that are not related.