- Home /
[Unity iOS] What's causing my draw call count to rise sharply?
I've spent a while researching iOS optimization and have so far implemented dynamic batching. My scene consists of several dozen game objects (about 150 on average), which form a puzzle and rotate around the scene. 7 textures are involved, so I've combined them into a single texture atlas, which is used by a single material, shared between all objects.
This definitely helped, as my draw calls dropped from over 200 down to about 16. However, I'm having a problem, where the draw call count seems to increase as more things happen in the scene.
A short case summary:
No object movement in the scene at all - only 16 draw calls
Camera is orbiting the objects - now there's 20 draw calls (why?)
Objects are rotating (game is running) - draw calls jump to 40, 80 or even 200+
The worst part is, the draw call jump is seemingly random. It isn't after X rotations or X amount of time, but appears to be completely at random. Sometimes it only increases to about 40 or 80, but typically no more than 10 seconds into playing, the draw calls are up in the 160-200 range, and continue to jump around that area. Also, restoring the game objects to their original positions and stopping all movement does not bring the draw call count back down - once it goes up, it stays up.
I'm still digging for a specific cause of the problem, but my question is, what could possibly be causing this? The material is not being changed at all, the textures aren't changing... the only changes are the game objects rotating, and some parent-child hierarchies being moved around.
Thanks for any help or advice!
[EDIT:] The solution has been found! The problem was scaling! All game objects had uniform scaling (depending on the puzzle), however, they were always values other than the default (1,1,1). Now, when all objects are instead always set to the default scale, the issue has disappeared.
Through tests, I have shown that scaling several objects causes the draw call count to jump, while restoring every game object back to (1,1,1) drops the draw calls to their original number. In my case, I'm generating the meshes procedurally, so instead of using transform scale, I am now simply scaling the mesh's vertex data directly. Problem solved. I hope this helps other people.
TL;DR - Having game objects at scales other than (1,1,1), EVEN if all objects share the same uniform scale, will break dynamic batching when combined with rotations. Rotating objects get batched together, and objects of the same scale get batched together, but evidently, non-one scaled objects that rotate will break batching. Thanks for the help everyone!
Just in case, you might write a loop to test on a keypress that that all materials are the same. Had you not said "the material is not being changed at all," that would be my #1 candidate for this behavior.
I agree, I assumed it must be the material, but they must all be shared because if I change the texture on the original material at any time, every game object updates to show it.
Answer by crogersKixeye · May 21, 2013 at 10:24 PM
Are you using lights? Unity's regular pixel-lit shaders will do a pass per light (i think)
Try putting an unlit shader on it and see if its drawcalls reduce.
Scaling objects will break dynamic batching. when the draw calls jump, does the "saved by batching" number go down?
Scaling is a small factor, you're right. I've made sure all objects have the same scale now, and it has reduced draw calls in some cases, but the main problem persists.
I'm using 2 directional lights, which is one potential source I haven't tested yet, I'll try leaving them out tomorrow and see what I get. The "saved by batching" number does go down, and always inversely proportional to the draw calls.
Thank you for your answer! I'll give an update tomorrow.
As per the edit in my original question, your last point was correct, scaling (when mixed with rotation) is what caused the problem, even though it was uniform. Dynamic batching only works on rotating objects that are scale (1,1,1) specifically.
Absolutely, I just wasn't sure how to do that. :P Thanks.