What is the difference between combine and static batching in Unity?

Combining Meshes vs. Static Batching in Unity: A Deep Dive

Quick answer
This page answers What is the difference between combine and static batching in Unity? quickly.

Fast answer first. Then use the tabs or video for more detail.

  • Watch the video explanation below for a faster overview.
  • Game mechanics may change with updates or patches.
  • Use this block to get the short answer without scrolling the whole page.
  • Read the FAQ section if the article has one.
  • Use the table of contents to jump straight to the detailed section you need.
  • Watch the video first, then skim the article for specifics.

The core difference between combining meshes and static batching in Unity lies in how they achieve draw call reduction, their implementation, and their impact on the game’s performance. Combining meshes physically merges multiple meshes into a single, larger mesh, typically done at runtime through scripting. Static batching, on the other hand, is a Unity feature that groups static (non-moving) GameObjects with the same material during the build process or at runtime, without actually modifying the underlying mesh data.

Understanding the Nuances

Combining meshes is a more direct approach. You literally weld vertices together to create a single mesh object. This reduces draw calls because the engine only needs to render one object instead of several. This technique is best used for objects that are procedurally generated at runtime or where the number of objects is large.

Static batching is Unity’s built-in solution. When enabled, Unity analyzes the scene and groups static GameObjects that share the same material into batches. These batches are then rendered in a single draw call. This is particularly beneficial for environments made up of many individual static assets.

Key Differences Summarized

Here’s a table summarizing the key differences:

Feature Combining Meshes Static Batching
—————– ————————————————– —————————————————
Implementation Scripted mesh manipulation and merging Unity’s built-in feature, either at build time or runtime
Mesh Modification Yes, meshes are physically merged No, meshes remain separate objects
Object Mobility Not Suitable, meshes are one object now Only Suitable for static objects
Material Requirement Generally used for objects that can share the same material after combining Requires objects to share the same material
Performance Trade-offs Increased memory footprint, higher initial processing cost Low memory footprint, efficient for static scenes
Use Cases Procedurally generated meshes, runtime assembly Static environments, large scenes with static objects

When to Use Which

  • Combining Meshes: Use this when you’re creating meshes dynamically at runtime and want to reduce draw calls for these newly formed structures. This is ideal for procedural generation.
  • Static Batching: If your scene is primarily composed of static objects (buildings, terrain pieces, etc.) that won’t move during gameplay, static batching is the better option. Simply mark the objects as static in the Inspector, and Unity handles the rest.

Benefits and Drawbacks

Combining Meshes

Benefits:

  • Significant draw call reduction when done correctly.
  • Useful for runtime-generated geometry.
  • Can simplify complex object hierarchies.

Drawbacks:

  • Increased memory usage due to the larger combined mesh.
  • More complex to implement and debug.
  • If any part of the combined mesh needs to be updated, the entire mesh needs to be recalculated which could lead to performance issues.
  • Can be difficult to manage if the scene needs to be modified after meshes are combined.

Static Batching

Benefits:

  • Easy to implement: Just mark objects as static.
  • Automatic optimization: Unity handles the batching process.
  • Reduces draw calls without significantly increasing memory usage.

Drawbacks:

  • Only works for static objects: Any movement disables batching for that object.
  • Requires objects to share the same material.
  • Can be less effective in dynamic scenes with lots of moving parts.
  • Each static batch can include up to 64000 vertices. If there are more, Unity creates another batch.

FAQs on Combining Meshes and Static Batching

1. What exactly is a draw call, and why are we trying to reduce them?

A draw call is a command sent from the CPU to the GPU telling it to render an object. Each draw call has overhead associated with it, so reducing the number of draw calls can significantly improve performance, especially on lower-end devices.

2. How do I enable static batching in Unity?

Simply select the GameObject in the scene and in the Inspector window, check the “Static” checkbox at the top. You can specify further what this object should be static for (Navigation, Batching, Occlusion, etc.). You can also do this for multiple selected GameObjects at once.

3. Can I use static batching on mobile platforms?

Yes, static batching is highly recommended for mobile platforms as it helps reduce the processing load on the device’s GPU.

4. Is it possible to combine meshes at runtime?

Yes, using scripting, you can combine meshes at runtime. Unity provides methods like CombineMeshes() to achieve this. Remember to manage memory and performance implications carefully.

5. What happens if I move an object that is part of a static batch?

Moving an object that is part of a static batch will break the batching for that object. The object will then be rendered in a separate draw call.

6. Does static batching work with different materials?

No, static batching requires objects to share the same material. If objects have different materials, they cannot be included in the same static batch.

7. How does GPU instancing compare to static batching?

GPU instancing is ideal for rendering many instances of the same mesh with different properties (e.g., color, scale, position). Static batching is designed for reducing draw calls on static, non-identical meshes that share the same material. GPU Instancing is particularly awesome for being dynamic.

8. What are the limitations of static batching?

Each static batch can include up to 64000 vertices. If there are more, Unity creates another batch.

9. Can dynamic batching be used in conjunction with static batching?

Yes, dynamic batching can be used alongside static batching. Dynamic batching batches moving objects with the same material, while static batching handles static objects.

10. How can I monitor the number of draw calls in my Unity project?

Use the Stats window in the Game view (Window > Analysis > Profiler, then select the Rendering section). This displays the number of draw calls, batches, and other performance metrics.

11. Does combining meshes always improve performance?

Not always. While it reduces draw calls, it can increase memory usage and initial loading times. It’s crucial to profile your project to ensure it’s actually beneficial.

12. Can I undo a mesh combine operation?

If you’ve combined meshes through scripting, you’ll need to write code to separate them again if needed. It’s a good practice to keep the original meshes as separate assets in your project.

13. What is the best practice for managing materials when using static batching?

Ensure that all static objects that you want to batch together use the same material instance. Avoid using different copies of the same material, as this will prevent batching.

14. Are there any alternatives to static batching and mesh combining for draw call reduction?

Yes, other techniques include:

  • GPU Instancing: For rendering multiple copies of the same mesh.
  • Occlusion Culling: To prevent rendering objects that are not visible.
  • LOD (Level of Detail): To reduce the complexity of meshes based on their distance from the camera.

15. How does Unity’s SRP (Scriptable Render Pipeline) affect batching techniques?

SRPs like the Universal Render Pipeline (URP) and the High Definition Render Pipeline (HDRP) offer more control over rendering, which can improve the efficiency of batching and instancing. These pipelines are optimized for different hardware configurations. Consider visiting GamesLearningSociety.org to learn more about game development techniques and their impact on performance.

Conclusion

Choosing between combining meshes and static batching depends on the specific needs of your project. Static batching is generally the preferred approach for static environments due to its ease of use and low overhead. Combining meshes is more suitable for dynamically generated geometry or situations where precise control over the mesh data is required. Understanding the benefits and drawbacks of each technique will help you make informed decisions and optimize your Unity projects for performance. Ultimately, profiling your game and understanding its unique needs is paramount to choosing the best optimization techniques.

Leave a Comment