Optimizing Performance: Static vs Dynamic Batching in Unity

 Static Batching:Static batching is a performance optimization technique in Unity that combines multiple static (non-moving) objects into a single draw call. When objects are marked as static, Unity can precompute the lighting and shadows for them and combine them into a single mesh. This results in fewer draw calls and reduces the overhead on the CPU, which can improve performance and frame rate.

To use static batching in Unity, follow these steps:

  1. Ensure that the objects you want to batch are marked as static. You can do this by selecting the object in the Hierarchy window and checking the “Static” checkbox in the Inspector window.
  2. Enable static batching in Unity’s Player Settings. Go to Edit > Project Settings > Player and scroll down to the “Other Settings” section. Check the “Static Batching” checkbox.
  3. Build your project. Unity will automatically batch the static objects during the build process.

Note that not all objects can be batched using static batching. Only objects that share the same material and have the same vertex format can be combined into a single draw call. Also, dynamic objects (objects that move or change shape at runtime) cannot be batched using static batching. In these cases, you can use dynamic batching or other optimization techniques to improve performance.

Pros and Cons of using static Batching:Pros of using static batching in Unity:

  1. Improved performance: By combining multiple static objects into a single draw call, static batching reduces the number of draw calls and the overhead on the CPU, which can improve performance and frame rate.
  2. Precomputed lighting: Static batching allows Unity to precompute the lighting and shadows for static objects, which can improve the visual quality of your game.
  3. Easy to use: Static batching is a simple and easy-to-use performance optimization technique in Unity. You just need to mark the objects as static and enable static batching in the Player Settings.

Cons of using static batching in Unity:

  1. Limited flexibility: Static batching works only for static objects that don’t move or change shape at runtime. Dynamic objects or objects with complex animations cannot be batched using static batching.
  2. Increased memory usage: Static batching increases the memory usage of your game, as Unity needs to create and store the combined meshes for static objects.
  3. Limited scalability: Static batching may not be as effective for large scenes or complex objects, as the combined meshes can become too large and may not fit into the available memory.
  4. Reduced batching efficiency: Static batching may reduce the batching efficiency of your game if not implemented correctly. For example, if objects with different materials or textures are combined, Unity may need to break them into separate draw calls, which can negate the benefits of batching.

Overall, static batching is a useful performance optimization technique for static objects in Unity. However, it’s important to use it wisely and test it thoroughly to ensure that it’s improving the performance of your game.

Dynamic Batching:Dynamic batching is another performance optimization technique in Unity that combines multiple small dynamic (moving or changing) objects into a single draw call. It works by temporarily combining the mesh data of dynamic objects at runtime, which can reduce the number of draw calls and improve performance.

To use dynamic batching in Unity, follow these steps:

  1. Ensure that the objects you want to batch are marked as “Static” and “Batchable” in the Hierarchy window. You can do this by selecting the object and checking the “Static” and “Batching Static” checkboxes in the Inspector window.
  2. Enable dynamic batching in Unity’s Player Settings. Go to Edit > Project Settings > Player and scroll down to the “Other Settings” section. Check the “Dynamic Batching” checkbox.
  3. Ensure that the objects you want to batch are not too complex or have too many vertices. Unity has a limit on the number of vertices that can be batched using dynamic batching, which is typically around 900 vertices.
  4. Test and optimize your game to ensure that dynamic batching is improving performance. You can use the Unity Profiler to measure the number of draw calls and the performance impact of dynamic batching.

Note that dynamic batching has some limitations and may not work in all situations. For example, objects with different materials or textures cannot be batched together using dynamic batching. Additionally, dynamic batching may not be as effective as static batching for large scenes or complex objects. Therefore, it’s important to test and optimize your game to find the best performance optimization techniques for your specific project.

Pros and Cons of Static Batching:

Pros of using dynamic batching in Unity:

  1. Improved performance: By combining multiple small dynamic objects into a single draw call, dynamic batching reduces the number of draw calls and the overhead on the CPU, which can improve performance and frame rate.
  2. Flexibility: Dynamic batching can be used for dynamic objects that move or change shape at runtime. This can be useful for objects with simple animations or particle effects.
  3. Easy to use: Dynamic batching is a simple and easy-to-use performance optimization technique in Unity. You just need to mark the objects as batchable and enable dynamic batching in the Player Settings.

Cons of using dynamic batching in Unity:

  1. Limited scalability: Dynamic batching may not be as effective for large scenes or complex objects, as the combined meshes can become too large and may not fit into the available memory.
  2. Increased memory usage: Dynamic batching increases the memory usage of your game, as Unity needs to create and store the combined meshes for dynamic objects.
  3. Reduced batching efficiency: Dynamic batching may reduce the batching efficiency of your game if not implemented correctly. For example, if objects with different materials or textures are combined, Unity may need to break them into separate draw calls, which can negate the benefits of batching.
  4. Limited optimization potential: Dynamic batching may not provide as much optimization as other techniques, such as static batching or GPU instancing.

Overall, dynamic batching can be a useful performance optimization technique for small dynamic objects in Unity. However, it’s important to use it wisely and test it thoroughly to ensure that it’s improving the performance of your game. It may not be suitable for all situations and may need to be combined with other optimization techniques to achieve the best results.

Comparing STATIC Vs. DYNAMIC BATCHING:

Static and dynamic batching are both performance optimization techniques in Unity that aim to reduce the number of draw calls and improve the overall performance of your game. However, they have some key differences that make them suitable for different types of objects and situations.

Static Batching:

Pros:

  • Effective for large, static objects, such as buildings or terrain.
  • Precomputed lighting and shadows can be used, which improves the visual quality of your game.
  • Simple and easy to use.

Cons:

  • Limited scalability, as the combined meshes can become too large for some systems.
  • Only works for static objects that don’t move or change shape at runtime.
  • Increased memory usage.

Dynamic Batching:

Pros:

  • Effective for small, dynamic objects that move or change shape at runtime.
  • Flexibility, as it can be used for objects with simple animations or particle effects.
  • Easy to use.

Cons:

  • Limited scalability, as the combined meshes can become too large for some systems.
  • Only works for dynamic objects.
  • Increased memory usage.

Overall, both static and dynamic batching can be useful performance optimization techniques in Unity, but they are best used for different types of objects and situations. It’s important to choose the right technique for your project and to test and optimize your game to achieve the best results. Additionally, it’s worth noting that there are other optimization techniques available in Unity, such as GPU instancing and culling, that can also be used to improve the performance of your game.

For more details, see https://bleedingedge.studio/blog/

Comments

Popular posts from this blog

Godot vs Playmaker: Which Game Development Tool Should You Choose?

Unlocking the Potential of Unity WebGL: A Game-Changer in Browser Gaming

Unraveling the Legend: Exploring the Mystique of The Legend of Zelda