- If I call the CombineMesh function from Start() or Coroutine, the result mesh always has vertex count == 0.
- The input meshes are validated correctly. I can even create Entities (Unity ECS) to render the mesh instead of MeshFilter, correctly. I had debug the code line by line, each input mesh has a lot of vertices but the result is always 0.
- If I call from the context menu, it works normally (successfully), even when pausing the player and call the function from frame 1.
//
public void Start()
{
// doesn't work
this.TestCombineMesh();
}
// works if called from context menu
[ContextMenu("Test combine mesh")]
public void TestCombineMesh()
{
// assume getting a game object with a lot of mesh filter children
GameObject source = new GameObject();
this.CombineMesh(source);
}
//
// assume the input game object has a lot of mesh filter children
public void CombineMesh(GameObject theObj)
{
theObj.transform.position = Vector3.zero;
MeshFilter[] filters = theObj.GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[filters.Length];
for (int i = 0; i < filters.Length; ++i)
{
combine[i].mesh = filters[i].sharedMesh;
combine[i].transform = filters[i].transform.localToWorldMatrix;
}
Mesh newMesh = new Mesh();
newMesh.CombineMeshes(combine);
Debug.Log("vertex count " + newMesh.vertexCount);
GameObject goNew = new GameObject("combined_" + theObj.name);
goNew.transform.position = Vector3.zero;
goNew.AddComponent<MeshFilter>().sharedMesh = newMesh;
goNew.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Universal Render Pipeline/Simple Lit"));
}
Is this a bug? Any reason for the CombineMesh result empty mesh? Anyway to workaround this?
Thank you very much!
p/s: It results empty mesh if being called from Update()
or OnGUI()
too