- Home /
CombineMeshes() Not Working Properly?
Hello Everyone,
I'm trying to combine three cubes into one mesh and keep them as individual submeshes to preserve their individual material's. However, when I combine the three cubes under the parent object, they all use the parent's material, even though I've set mergeSubMeshes to false.
Thanks for the help!
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class Test1 : MonoBehaviour {
void Start() {
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>() ;
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length) {
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.active = false;
i++;
}
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine,false, false);
transform.gameObject.active = true;
}
}
I didn't run your code, but looks like you're doing it right. Setting false to mergeSub$$anonymous$$eshes should keep the materials. Have you trying to access sub$$anonymous$$eshCount to check if you have 3 submeshes? if you do you could change the materials manually (I know that Combine$$anonymous$$eshes should do it for you).
I have confirmed that there are 3 sub-meshes using sub$$anonymous$$eshCount. I'll try to manually re-assign the materials to the sub-meshes, but I feel that this is going to make merging meshes dynamically really performance heavy.