Course contentsShow
← Unity Game Development
Lesson 734 of 82811. Performance and Data PersistenceFree lesson

Implementing Dynamic Batching for Moving Objects

Learners will identify and organize small, moving objects to enable automatic dynamic batching, optimizing performance for frequently updated game entities.

In this lesson, you will learn how to structure your game entities to leverage Unity’s dynamic batching, reducing draw calls for frequently moving objects.

Understanding Dynamic Batching

While static batching combines objects baked into the scene, dynamic batching is a runtime optimization that automatically groups small, moving meshes into a single draw call. Unity performs this by transforming the vertices of these objects into world space on the CPU before sending them to the GPU. Because this calculation happens every frame, it is only efficient for very small meshes (typically fewer than 900 vertex attributes).

Constraints for Success

To enable dynamic batching for moving entities, you must ensure your objects meet strict criteria:

  1. Shared Materials: All objects must use the exact same material instance. If one object has a unique shader property, the batch will break.
  2. Mesh Complexity: Keep vertex counts low. Complex meshes increase CPU overhead, often negating the performance gains of batching.
  3. Uniform Scaling: Objects should ideally use the same scale. If you have objects with different scale factors (e.g., (1,1,1) vs (2,2,2)), dynamic batching will fail for those instances.

Implementation Checklist

To organize your moving objects, focus on a "batch-friendly" architecture. Instead of managing thousands of unique small props, consolidate them into a shared material atlas.

// Ensure all projectiles or small debris share the same material
void Start() {
    // Avoid modifying material properties per-instance
    // This triggers a 'Material Property Block' or a new material clone, 
    // effectively disabling batching.
    renderer.sharedMaterial.color = Color.red; 
}

If you notice in the Frame Debugger that your "Draw Mesh" calls are not grouping together, check if your objects are accidentally using different material instances or if the vertex count exceeds the engine's threshold. By keeping your meshes simple and your materials identical, you allow Unity to combine these moving parts into a single submission to the GPU, significantly lightening the load on your render thread.

Key Takeaway: Dynamic batching reduces draw calls for moving objects by grouping meshes with identical materials and low vertex counts, provided they share uniform scale and do not require individual material property modifications.