Just battled with what looked like a great and simple function for combining meshes.
The basic example is great (once you update it to SetActive()) and as long as you don’t have multiple materials on the objects.
OK what you would like CombineMesh() to do is what you see in the example, take any group of meshes and submeshes provided and create a new mesh.
But it’s not quite that smart if you have sub meshes you have to extract them and combine the submesh groups (one for each material in the meshes being provided).
Now you have x meshes one for each submesh / material in the meshes you need to combine.
So you now combine these meshes into a single mesh!
This is the hacked together code I ended up using just to combine a group of meshes with two materials and I thought I was an OK programmer!!!
void CombineMeshes()
{
transform.GetComponent<MeshFilter>().mesh = null;
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
List<CombineInstance> combineList0 = new List<CombineInstance>();
List<CombineInstance> combineList1 = new List<CombineInstance>();
Debug.Log("CombineMeshes "+meshFilters.Length);
int i = 0;
int j = 0;
while (j < meshFilters.Length)
{
Debug.Log("Mesh index " + j);
if (meshFilters[j].mesh != null)
{
CombineInstance combine = new CombineInstance();
combine.mesh = meshFilters[j].sharedMesh;
combine.transform = meshFilters[j].transform.localToWorldMatrix;
Debug.Log("Mesh " + j + " sub count" + combine.mesh.subMeshCount);
combineList0.Add(combine);
combine = new CombineInstance();
combine.mesh = meshFilters[j].sharedMesh;
combine.transform = meshFilters[j].transform.localToWorldMatrix;
combine.subMeshIndex = 1;
combineList1.Add(combine);
}
meshFilters[j].gameObject.SetActive(false);
j++;
}
// Now combine the seperate sub meshes to two material sub meshes
CombineInstance[] combineUs = combineList0.ToArray();
Mesh mesh0 = new Mesh();
mesh0.CombineMeshes(combineUs, true, true);
combineUs = combineList1.ToArray();
Mesh mesh1 = new Mesh();
mesh1.CombineMeshes(combineUs, true, true);
CombineInstance[] combine2 = new CombineInstance[2];
combine2[0] = new CombineInstance();
combine2[0].mesh = mesh0;
combine2[1] = new CombineInstance();
combine2[1].mesh = mesh1;
//combine2[1].subMeshIndex = 1;
// Then combine the two material sub meshes note the Combine mesh flags used
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine2, false, false);
transform.GetComponent<MeshCollider>().sharedMesh = transform.GetComponent<MeshFilter>().mesh;
transform.gameObject.SetActive(true);
}
I know there are things on the asset store but with just a little more work this method could do the above in a single line as in the example.
As opposed to n+1 calls per number of materials/submeshes.