How to work with 2D atlases?

I have a 2D game.
In Unity, it is recommended to place all images in a 2D atlas. This reduces the number of draw calls, reduces the load on the video system and increases fps.

And everything seems to be fine, but:

  1. It is recommended to use 1 atlas for each scene. Multiple atlases force Unity to switch between them, reducing performance and fps.
  2. When using any image from the atlas, the entire atlas will be loaded.
  3. If there are several atlases where the same images are loaded, when using these images in the scene, any of these atlases will be loaded (random selection).

As a result, problems arise. Here is a simple example.
I have images of trees and flowers. In scene 1 I need trees and flowers, in scene 2 only trees.

I decided to create 2 atlases.
In atlas1 I add pictures of trees.
In atlas2 I add pictures of flowers.
I load scene 2, where there are trees and flowers. Scene 2 loads 2 atlases. We get additional “video system load” because 2 atlases cause switching between them.

I decided to create an atlas for each scene.
In atlas1 I add pictures of trees and flowers.
In atlas2 I add pictures of trees.

I load scene 2, where there are only trees. Scene 2 loads atlas1 with trees and flowers. We get additional load because the atlas contains an image of flowers.
This happens because the tree prefabs in Image contain a link to the image of trees. The image of trees is added to 2 atlases.
Unity randomly chooses from two atlases and this time atlas1 was chosen.

What to do? Duplicate, copy images so that they are unique for each scene, create unique prefabs only for one scene - an extremely bad option.
This is duplication of images, prefabs, components…

I think you’re over-optimizing at this point. I would have an atlas for trees and another one for flowers. Having multiple atlases active in 1 scene will be fine.

The gpt chat strongly recommends using 1 or 2 atlases per scene. Three atlases are already a questionable solution.

I only gave an example to make it clear.
In reality, I will have many objects - trees, flowers, rocks, clouds, furniture…

So, even grouping all the objects by category, I will get 100 atlases.

On scene 1, I will need trees, flowers, clouds, furniture.
On scene 2 - clouds, rocks, furniture.

I think you understand the huge problem. A huge number of object combinations.

I was even ready to duplicate all the images. Just copy the images of the trees - create copies of the folders for each scene. And add copies of the folders to the atlas of each scene.
The scenes have identical trees, but because of the atlases, I am forced to copy the pictures, create copies of the prefabs on each scene…

And even so, there will be problems.
Some parts of the scenes - transitions between them (like a vestibule, imitation of seamless scenes where another scene is loading) are made as prefabs.

That is, part of the scene as a prefab is added to two scenes - where the player is coming from and where he is going. In total, 2 such prefabs will give 2 atlases and scene 1. Already 3.

It is absolutely unclear how to work with atlases.
They destroy the entire ideology of prefabs, reusing objects, and incredibly complicate the development of the level.

Well I can tell you ChatGPT is wrong to strongly recommend that in this case. In general try to group up sprites that will be in the same scene as much as possible, but if there a lot of them mixed in different scenes then it’s not a big deal, just group them as best as you can. Are you even having performance issues in the first place?

To optimize Sprite Atlas usage, ideally all or most Sprites that are active in the Scene should belong to the same Atlas. It is good practice to split Sprite Textures into multiple smaller Atlases according to their common usage.

It’s the same here - it’s advisable to make 1 atlas per 1 scene.

But then it is impossible to reuse prefabs on other scenes and it is simply impossible to develop a game.

The paradigm of prefabs, reuse, inheritance (this is object-oriented programming) - which is also implemented in Unity becomes impossible.

I cannot use the tree prefab on scene 2 because it is in the atlas of scene 1. Copy everything in a row and multiply entities - pictures, prefabs…?

I have 100 different objects in the game. And this is very little.
And each scene uses different objects, repeating objects.

What and in which atlas should I add? It is impossible to calculate.

You should be atlas-ing sprites based on profiler testing, not doing it preemptively. No point doing all this optimisation if there is no performance issues to begin with.

1 Like

Do your images all fit in a 2k or 4k atlas texture? If so, put them all in one.

I have a project with 6,000 tiles sized 32x32 and they all fit into a single texture. Doesn’t get any faster and more convenient than that!

My images are big and small, beautiful and not so. There are tiles and squares… but the question was completely different, don’t you think so?

I make 2d games for mobile for a wile now, and I load lot more than 2 atlases in memory without problem. There is not an unique solution to define how to group images on your atlases, it depends on your usage.

Atlases are here to help you maintain low RAM memory usage, as you can compress atlases with format that can be kept compressed in video memory, And decrease your draw call by grouping them if image are on the same atlas.

Now you have to use the frame debugger, the memory profiler and your mind to find the best way to group your atlases for your game.
Remember to profile on builds on your target device to have the real usage of your game and not the editor one :wink:

1 Like