Sprites (2d and UI) Atlas not reducing number of draw calls

Hello all,

I followed the following tutorial to use a sprite atlas to reduce the number of draw calls for my UI elements:

What is advised is in line with the 2017 Unite UI optimization session I saw as well:

(around 20-25min mark)

I followed the steps and created an atlas out of the UI sprites I have, which I collected into a single folder:

I also ensured that each UI gameobject actually uses the sprites located in the folder that I fed into the Sprite Atlas.

The tutorials are saying once you feed the folder and press pack preview, you are done, Unity itself will take care of finding those “fed” sprites and use the atlas instead so that you get less draw calls. However, my simple scene above with these UI icons has 15 draw calls, 9 of which are from the UI:

Now I am using different canvases for each UI element to understand the “dirtying” aspect with animations, however for the particular health bar, everything is under the same canvas and they should be sharing this atlas which they clearly are not. According to the tutorials above, the “Different Material Instance” should not be a batch preventer, when the sprite atlas is used.

Could you please tell me what I am doing wrong?
-Edit: Dynamic batching and sprite batching are enabled in the relevant settings…

I’m 99% certain batching is broken. I’ve been working on this for hours and can’t find out why the batches don’t work. In the frame debugger is just says “reason unknown.” I’ve set it up exactly as the documentation says and it doesn’t work.

I’ve did a quick test following the instruction from the blog posted in the first message and all works ok on 2022.3.31f1 (for the bellow math ignore the default 1 batch that is present in the default unity scene)

packing textures or folders will combine the batch call. for example 4 images will get the batch number from 4 to 1

this works also if images are inside the UI canvas as individual images.

but if you have different canvases then it works differently. multiple canvas objects will not combine in one canvas for the batch call. I don’t have the time to watch that 2017 video presentation but did he said it will combine multiple canvases?

from my tests:

single atlas of 10 images, each attached to 10 images UI objects in one canvas = batch 1
same atlas of 10 images but we have 2 canvases with 5 images each = batch 2

but all goes to hell because once you start adding UI elements those will create a lot of different batches.

for example, adding 3 legacy buttons (and you use atlas packed images inside the button image field) then it will give you 6 batch calls. but if you remove the text game object, then all those 6 batches will vanish and get back to 1 batch.

conclusion, internally unity will not pack their UI elements inside atlases so any unity created internal sprite added to various Ui elements will mess the sprite atlas calculation.

I don’t want to test which Ui elements will not mess the batch call but Image UI object works as you expect, button without text also work. button with text, all goes to hell

I’m starting to get a clearer understanding. I’ve learned that Unity cannot batch any sprite dynamically such as assigning an image to card at runtime. I see now that multiple instances of one canvas also does not batch such as several instances of the same card which is a shame because every single aspect of the card is dynamic and cannot be batched. The only sprite that does not change is the back of the card and it is the only static sprite on that canvas so there is no reason to place it in a sprite atlas.

The only way this would work for my particular case is if I had multiple static sprites on the back of the card in the same canvas. But since the back of the card doesn’t change I would be better off just making it one sprite in photoshop. Seems like sprite batching is only for very specific cases and types of games which is a shame because multiple instances of meshes can batch just fine.

may ask which platform you need to publish the game that these UI batches are so critical for you?

what are the details of your project and why do you need this optimization?

from my understanding this batch thing is more of a method for scene objects and less for UIs

This does NOT sound correct to me. Maybe there’s something wrong with your setup.

https://docs.unity3d.com/Manual/dynamic-batching.html

I’ve had huge dynamically batched scenes world and UI before.

You need each item drawn in order to share the same parameters in order for it to batch.
You can’t have ImageA (from texture1) and ImageB (from texture2) and ImageC (from texutre1) and expect that to batch. From what you’ve shown, this …should…be ok. The material needs to be the exact same, no other parameters changed.

The other thing would be a regression bug. I know they have fiddled with SpriteAtlas recently. What version of Unity is this? And have you tested in a build?