- Home /
[C#] Using CombineMeshes and preserving originals
Hi, I'm using CombineMeshes - based on the example in the documentation. When I trigger the code the new mesh is created, but the original meshes are removed (it appears).
What is the best way to prevent this?
For my prototype I have one object made of many sub objects, each with their own mesh. I would like to make a new object that has a single mesh that looks the same as all those smaller meshes.
It seems to me that I could just quickly duplicate the original object, then run the combine on that - but perhaps there is a better, more performant way?
Here's the code so far (runs in an onCollisionEnter) - it's a horrible mangle of existing code, I just wanted to test the combine meshes stuff out!
MeshFilter[] meshFilters = collision.collider.transform.parent.GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
Debug.Log("meshes found: "+meshFilters.Length);
while (i < meshFilters.Length) {
Debug.Log("combining "+i);
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.active = false;
i++;
}
GameObject targetObject = GameObject.Find("TargetMesh");
Debug.Log("Found: "+targetObject);
targetObject.transform.GetComponent<MeshFilter>().mesh = new Mesh();
targetObject.transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
targetObject.transform.gameObject.SetActive(true);
Thanks, Martin
FYI I moved away from this approach, so never found an answer.