In quick tip https://unitytips.wordpress.com/2014/05/28/quick-tip-combining-meshes/ we provided a script for combining meshes and a simple example on how to use it.
To remind us about the script, we have it’s editor menu here:

We can see that the “Combine When Launched” option is selected. This is the default setting. If we select this option, the script will run everytime at the beginning of our game and automatically combine the meshes. We can however do this manually as well by clicking the “Combine” button. Once we do that, we get a menu with only one button “Uncobime”. This serves us to undo our action if we decide that we want to add more objects to the group later on and want to launch the script again.
Let’s take a look at how the script actually works and what lies beneath the surface.
The script first takes all the MeshFilter components from all the child objects of our group by calling:
this.transform.GetComponentsInChildren<MeshFilter>();
Then it starts to create new groups of objects. These are based on materials, which the original meshes use. Therefore it creates as many groups as there are different materials used within our group of objects. When it’s done sorting the meshes into newly created groups, it starts to combine them. First the script saves all the MeshFilters used for combining within its private field. These will be later used for undoing the combining process. Afterwards, it calls the CombineMeshes() function and passes it, one by one, the various groups of meshes as a parameter. This function creates our new, combined meshes. When our script is done combining, it disables all the original MeshFilters. The reason for doing this is so that only the new meshes get drawn and not the old ones (that would not save any draw calls, so it makes sense). Once the meshes are combined, the script saves the newly combined meshes within another private field, which is again used when undoing its action.
Why did we chose this approach? There are different ways of dealing with combining meshes that can be found online. Some of them do not take multiple textures into account, some of them try to create texture atlases and reduce the amount of draw calls even further. We wanted an easy-to-use tool that works as expected and is easily understandable. Another thing to mention is dealing with the original meshes. We have encountered scripts that destroy the original objects once they are done combining. However, this does not seem like a good idea. The objects can contain other valuable components than only the MeshFilter, such as colliders, audio sources and others. These don’t have to depend on the geometry of the mesh itself, but rather on the position of the player (i. e. a noise plays when a player comes near to an object). We wanted to keep these, so we decided to just disable the MeshFilter and leave the rest of the object as is. Another reason is that we want to be able to undo the combining. Obviously, this wouldn’t be possible if we destroyed the objects.
So here you can see how the script actually works and that it’s quite easy to understand it’s purpose. If you have any other questions about mesh combining, don’t hesitate to ask.


















