How static batching works

Hi everyone,

I need to use the StaticBatchingUtility to batch all the static GameObjects in my scene. The GameObjects have to be dynamic at start (so I could access and change meshes), so I can’t use unity’s default batching.

The problem is that my scene is pretty big, and I have lots of GameObjects scattered around the scene. I can’t just batch all the Objects with the same material to one mesh, because they might be far from each other and it is not effective.

As far as I understand, when unity does the static batching, the objects are batched in groups, and not in one big mesh. An I correct? If it’s the case, is there a way to see those groups? So I will be able to save them and than use them in my own batching.

If not, what is the best way to perform the batching?

Many thanks,
Anton

Static batching bakes your models into one huge mesh. This means that if you had 1000 wall segments, all using the same model, then static batched them, you’d be using 1000x memory than using a single model. It would reduce draw calls on the expense of used memory. Put simply: It creates one huge mesh out of several instances of smaller meshes.

When you render something, a few pieces of information is required, like what model to render, what indices of that model should be rendered and what material to use.

Unity will try to render as many parts of your staticly batched mesh as possible, using a shared material. If there is no shared material, and if many parts use different materials, Unity is forced to render each collection (based on material) individually.

Even if it’s one big mesh that is generated, not all of this is used to render it every frame. Unity will look at what renderers should be visible, and rebuilds an index buffer for your statically merged mesh. Put simply: Unity will exclude the vertices that belong to a renderer that is not visible when rendering the mesh, to avoid rendering things outside your frustum.

This process has some overhead of course since Unity has to rebuild indices every time a renderers leaves or enters the frustum. If you are using profilers you may see that it’s using some time for batching. This is typically what happens for static batching.

Batching should be used with consideration. If you are already running dangerously low on memory, turning static batching on may crash your app. Try to use few unique materials for batching as it allows Unity to reduce the number of draw calls. If batching is slow, consider reducing the number of triangles per mesh or number of renderers in your scene.

Static batching generally dramatically improves render time but may increase memory footprint.

You can see the static mesh if you have a static object and select its mesh. You can also find the mesh by going to the new Memory Profilers detailed view, or you can find it via Resources.FindObjectOfTypeAll.

i’m using StaticBatchingUtillity.Combine() in my project.
with this you can batch several models under virtual one model.(well not really virtual since you can see it in the hierarchy when the project run).
by this you should get less drawcall when drawing the batched object.
keep in mind that static object that you combine cannot be moved in scene.