- Home /
Sprite batching
So, I have a scene setup with lots of different sprites using the new 2d system. Most of the sprites are single and not from atlases. When I hit play, I have a crazy amount of draw calls. The odd thing is, if, while playing, I disable a chunk of sprites and then re-enable them, all of a sudden they are now batched and the draw calls drop. My question is, how come they are batched only after I disable and re-enable them?
Ins$$anonymous$$d of disable / enable gameObject, use renderer to do the same, like
renderer.enabled = false;// or true;
I don't want them disabled though. I just am confused on why the batching occurs only after a disable and re-enable
Did you ever figure this out? Seeing same issue. I think I might be related to using the "Sprites-Default" material for all sprites even though I have 3 different sprite textures (in my case I am using atlases, 3 @ 1024x1024), but not sure yet.
Answer by shopguy · Mar 17, 2015 at 05:47 AM
Old question, but no answer, so...
Sprite batching is related to drawing order, which in 2D world is a combo of your sorting layers and Z position. I'm guessing you have several (maybe all) of your objects with the same sorting layer and Z position, so Unity ends up drawing them in a seemingly random order (sometimes you'll notice overlapping sprites "flicker" in this case, as one is drawn first on some frames, and the other on other frames).
When you disable/re-enable, it changes the order Unity is drawing your sprites. So depending on which ones you enable/disable, you will see different results, and the results can be huge variances. Take for example, 2 sprites using 2 different textures:
Sprite A = texture 1 Sprite B = texture 2
If you have a 100 of each sprites, and Unity draws Sprite A, then Sprite B, then Sprite A, then Sprite B.. over and over, for each 100, you will end up with 200 draw calls. If Unity draws all of Sprite A first, then all of Sprite B second, you will have 2 draw calls.
This can get very difficult to manage in certain scenarios and become very complex in a hurry. For example, 2.5D games can be very tricky, as things need to be drawn in a specific order to fake 3D (lots of overlapping). If you try to share parts of your sprite sheets... say you have 10 characters all with the same eyes, so you try to share the same eyes from a single sprite sheet for all of those characters, but mix in other parts from other sprite sheets. If you draw eyebrows first (from sheet A), then eyes (from shared sheet), then face (from sheet A again), you get 3 draw calls, instead of 1.
The only really simple way to handle that I have figured out:
Have everything on 1 huge sprite sheet -- not practical unless you are doing pixel-art or low-res games.
Have all copies of the same sprite use the same Z order, always. So if you have a "spider" in your game, all spiders have the same Z order (or sorting layer), or at least don't have any other sprites "in between" them. This is what I usually do, but can be a pain to setup scenes this way, without writing some editor scripts to help.
Probably their are some assets/libraries to help -- never tried any.