Dynamic Mesh Combining in Unity with C#

Unity mesh combining final forest

Introduction

A few weeks ago I decided to make a forest fire simulator in Unity (you can test it here). A forest, as you know, has a lot of trees. But when I added all trees to the forest I realized that the simulation was running very slow. Then I recalled the game Cities: Skylines and began to wonder how that game was running faster despite having more trees than my small forest. After an unscientific research on the Internet I realized that the answer was mesh combining.

Mesh combining is the art of taking several of your objects in Unity and combine them into one object. So instead of having several tree game objects, you will end up with one big tree game object that contains several trees. This will increase the performance of your game or simulator. You can measure this by looking at the number of batches in Unity's "Statistics" window. The fewer batches, the better.

Unity already has a small tutorial on how to combine meshes (you can find it here). But that tutorial is way too basic. What if you need to remove a tree from the combined mesh? What if you need to add a tree to the combined mesh? So I decided to extend that tutorial with this tutorial.

  • In part 2 you will set up the basic scene and make a tool that looks like Cities: Skylines's Tree Brush tool.

  • In part 3 you will learn how to combine meshes with different colors into one mesh. You will not need this part to complete the rest of the tutorial, so you can safely ignore it if you want.

  • In part 4 you will learn how to make a forest and combine the trees into larger meshes.

  • In part 5 you will learn how to add and remove trees from the combined meshes, by using a tool similar to the Tree Brush tool in Cities: Skylines.