- Home /
Sprite batching
Is it possible to get sprite batching to work regardless of what order your sprites are drawn in? From testing it seems that every time a different sprite is going to be drawn, it ends the current batch. So if I have 3 sprites, 10 each, and I set the "order in layer" at random, I get lots of draw calls and not much batching. If I set them so that all 10 sprites of each type are drawn together, I get 3 draw calls, perfect batching.
I understand batching might not be possible/practical if the sprites actually overlap, but in my case I have some that overlap and some that don't. While I could write my own logic to order things in an optimized way, I'm hoping that something already exists in Unity.
I guess what I was hoping for is that Unity would at least batch sprites together if it noticed that they didn't overlap other sprites. For example, if it was "time" (z-index/layer) to draw a sprite, Unity might think "hey, I just drew a different type of sprite, before I draw this next one I'll go see if there are any others of the same type I can draw, so they'll batch together". I guess that might be difficult to do at the engine level. So I'll assume the answer is NO and accept ivomarel's answer because I think it will work for other future readers and is a good "general" answer, just not for my specific case.
Answer by ivomarel · Apr 04, 2014 at 12:40 AM
Yes, make sure that the sprites are in the same atlas. When you select a sprite, (Texture type should be sprite), look at the inspector and choose sprite mode 'Multiple'. Then, make sure you give the sprite a packing tag. Sprites with the same tag will be packed and batched together as soon as you run it in the Editor.
Note: You also have to set Edit > Project Settings > Editor > Sprite Packer Mode to 'Always Enabled'
I'll test this. Seems like one would want all in one batch, so always use same tag, unless this is subject to the max texture size in which case probably will not help me as my sprites are high res.
Didn't test but read docs and looks like this will be subject to max texture sizes, so will not work in my case. $$anonymous$$ost of my sprite sheets are already 1024×1024 per object. Either a single building, or several poses of one animated character. I'll have about 20 of these to stay under 20mb in texture RA$$anonymous$$.
This works as long as everything is in the same atlas. If you have sprites from atlas A at ordering 0 and 10, and some from atlas B at 5 and 15, the best you can hope for is four batches - A at 0, B at 5, A at 10, B at 15.
Old thread but sprite mode of $$anonymous$$ultiple doesn't matter for batching. That just deter$$anonymous$$es if there should be multiple sprites within the texture you are inspecting. But you can still batch 2 single sprites that have the same packing tag. If you have multiple sprites within 1 texture then definitely have it on but it doesn't involve batching.