- Home /
MeshFilter and CombineMesh Doesn't Build Properly
I have a script (below) that I'm using to combine the mesh of a series of child gameObjects to save resources. The combining of the mesh works perfectly while in Unity's Game Editor, however once I build the game and run it, the combined mesh is nowhere to be found. There also doesn't appear to be any related errors or warnings during the build.
I'm on latest Unity4. (by the way, the CombineMeshes documentation on Unity's site is wrong. It doesn't even compile!)
Here's the code I'm using.
#pragma strict
@script RequireComponent(MeshFilter)
@script RequireComponent(MeshRenderer)
function Start () {
var meshFilters = GetComponentsInChildren.<MeshFilter>(); // Get the MeshFilter components of the children
var combine : CombineInstance[] = new CombineInstance[meshFilters.Length-1]; // Create an array to hold the meshes
var index = 0;
for (var i=0;i<meshFilters.Length;i++) { // Create a loop to iterate through the "combine" array
if (meshFilters[i].sharedMesh == null) continue;
combine[index].mesh = meshFilters[i].sharedMesh; // Assign the mesh of this child to the sharedMesh
combine[index++].transform = meshFilters[i].transform.localToWorldMatrix; // Position the mesh
meshFilters[i].renderer.enabled = false; // Disable the renderer as it's no longer needed
}
GetComponent(MeshFilter).mesh = new Mesh();
GetComponent(MeshFilter).mesh.CombineMeshes(combine); // Get the combined mesh
renderer.material = meshFilters[1].renderer.sharedMaterial; // Assign the material
}
I'm still struggling with this 2 days later. Does anyone have any ideas on why the combined mesh doesn't work once the game is built, however works perfectly fine in the game editor?
Answer by huhudage · Sep 22, 2013 at 02:41 AM
Hi cassius, have you fixed this problem yet? I have the same problem now...
I'm not sure why but this one below is working for me. Not very different. It may have just ended up being a bug in the version of Unity I was using. I'm now on Unity 4.2.1f
#pragma strict
@script RequireComponent($$anonymous$$eshFilter)
@script RequireComponent($$anonymous$$eshRenderer)
function Start () {
var meshFilters = GetComponentsInChildren.<$$anonymous$$eshFilter>(); // Get the $$anonymous$$eshFilter components of the children
var combine : CombineInstance[] = new CombineInstance[meshFilters.Length-1]; // Create an array to hold the meshes
var index = 0;
for(var i=0;i<meshFilters.Length;i++) { // Create a loop to iterate through the "combine" array
if(meshFilters[i].shared$$anonymous$$esh == null) continue;
combine[index].transform = meshFilters[i].transform.localToWorld$$anonymous$$atrix; // Position the mesh
combine[index].mesh = meshFilters[i].shared$$anonymous$$esh; // Assign the mesh of this child to the shared$$anonymous$$esh
index++;
Destroy(meshFilters[i].renderer); // Remove the renderer as it's no longer needed
Destroy(meshFilters[i]); // Remove the mesh filter as it's no longer needed
}
transform.GetComponent($$anonymous$$eshFilter).mesh.Combine$$anonymous$$eshes(combine); // Get the combined mesh
if(renderer)
renderer.material = renderer.shared$$anonymous$$aterial; // Assign the material
}
Also, I should point out that every time I've used this the overall triangle count has gone up. See this thread which I started if you want to know about that particular problem.
Your answer
Follow this Question
Related Questions
Combine (skinned) submeshes causes increased vertcount(for each submesh the whole vertcount) 0 Answers
Mesh.CombineMesh() results in different heights between objects 1 Answer
500K upper bound on total number of meshes? 0 Answers
Combing Meshes not working as expected? 1 Answer
GetComponentsInChildren(MeshFilter) not recognizing sharedMesh as variable 2 Answers