Search code examples
unity-game-enginebatching

How does Static Batching work in Unity3D


I am working on a 360 video player app for VR Platform and looking forward to optimise it for mobile platforms. At this time I have a question regarding static batching.

Lets say I have a sphere which would never move even if the world ends. But a video is being played on it. That means texture of its material is being updated once a frame.

Do I mark this sphere as static for batching or leave it?

If I mark a moving object as static, would that affect performance?


Solution

  • Normally, each mesh in your scene is rendered one by one. Static batching will attempt to combine multiple meshes together, so that they can all be drawn together. This can potentially lead to dramatic improvements in draw call efficiency.

    Unity still keeps track of the individual GameObjects in each batch, which is useful in case they can be culled (further improving render performance), or in case they need to be removed from the batch.

    There are some conditions, though.

    Static geometry must not be moved. After the meshes have been combined together, moving any GameObject within the batch would break some assumptions about how that mesh was created. You can ask Unity to move static geometry (and it will try!), but doing so will cause tend to cause errors and slowdowns.

    All objects in a static batch must share a single material instance. Texture atlasing is fine, and editing the material's properties is fine, just as long as you keep a single instance of that material (note that calling renderer.material clones the material, so you may want to edit renderer.sharedMaterial instead).

    In most cases, the easiest way to activate static batching is by marking a GameObject as static in Unity's level editor. This will automatically trigger a static batching process during builds. If you need static batching in the editor, or if you are generating static geometry while the game is running, you can have a script use the StaticBatchingUtility class to create batches on the fly.

    If I mark a moving object as static, would that affect performance?

    It will affect performance and may cause errors. Don't do that.

    Do I mark this sphere as static for batching or leave it?

    Static batching only helps if you have multiple meshes that all share the same material instance.