Multiple SpriteSheets break batching

The problem:
I can’t fit all my 2d animation sprites for all units and environment in 1 spritesheet.

Everything batches fine until I use two spritesheets…
I noticed the following : all units from spritesheet A batch fine until something needs to be rendered fom spritesheet B.
so lets say from back to camera : 50units - tree -50 units - camera will result in 3 drawcalls… Not that bad but if you have:
unit- tree - unit -tree -unit -tree … like it’s probably most of the time… this wil result in 100+ drawcalls even though you have only two spritesheets…

How can you solve this? Or better, how can i use two spritesheets and have only two drawcalls even though the sprites are at different distances to the camera

Hope this image helps to understand the problem:

  • 1 : the trees are on spritesheet B and batch fine
  • 2: the batching is broken as the unitsprite sits on spritesheet A and is put in between the trees…

I can’t explain it very technically. And i’m not finding any documentation right now.
But the batching is correct.
The engine will batch all the sprites that are in the same atlas, and you can use any z value and it will batch (your trees). BUT if you put another sprite that is in a second atlas (your lance dude sprite) it will batch all the sprites from the first atlas (the trees) untill it tries to render the other atlas sprite (the lance dude) when it try to render the dude sprite it will start a second batching, that’s OK 2 draw calls. But then it will try to render another tree, and it’s in the first atlas so it starts another batching.

The lesson is, try to make atlas separate sprites by location in the scene: all background sprites, all middle sprites, all front sprites and don’t try to put a front sprite in the background because that will cause an additional draw call.

As you are using sprites i recommend using Sorting Layers instead of z positions, but the above still apply with sorting layers.

Edit: This link will give some more explanation Optimizing Performance in the “Or keep things ordered” section.