public static void CombineMeshes(UnityEngine.GameObject[] gos) { if (gos != null) { MaterialMeshes lMaterialMeshes = new MaterialMeshes(); for (int i = 0; i < gos.Length; i++) { UnityEngine.GameObject MaterialRoot = gos[i]; for (int j = 0; j < MaterialRoot.transform.childCount; j++) { UnityEngine.GameObject go = MaterialRoot.transform.GetChild(j).gameObject; MeshFilter lMeshFilter = go.GetComponent <MeshFilter>(); MeshRenderer lMeshRenderer = go.GetComponent <MeshRenderer>(); if (lMeshFilter && lMeshRenderer) { Mesh lMesh = lMeshFilter.sharedMesh; Material lMaterial = lMeshRenderer.sharedMaterial; if (lMesh && lMaterial) { lMaterialMeshes.Material = lMaterial; lMaterialMeshes.Meshes.Add(lMesh); lMaterialMeshes.MeshFilters.Add(lMeshFilter); lMaterialMeshes.Transform.Add(lMeshFilter.transform.localToWorldMatrix); } } } //XOUtil.Display(MaterialRoot, false) MaterialRoot.SetActive(false); } MergeMeshes(lMaterialMeshes, gos[0].transform.parent.gameObject); } }
private static UnityEngine.GameObject MergeMeshes(MaterialMeshes rMaterialMeshes, UnityEngine.GameObject mMergedParent) { if (rMaterialMeshes == null) { return(null); } if (rMaterialMeshes.Meshes.Count == 0) { return(null); } int lMeshCount = rMaterialMeshes.Meshes.Count; // Determine the position of the new merged object UnityEngine.Vector3 lCenter = UnityEngine.Vector3.zero; for (int i = 0; i < lMeshCount; i++) { lCenter.x += rMaterialMeshes.Transform[i].m03; lCenter.y += rMaterialMeshes.Transform[i].m13; lCenter.z += rMaterialMeshes.Transform[i].m23; } lCenter /= lMeshCount; // Create the list of meshes that will be merged CombineInstance[] lMeshes = new CombineInstance[lMeshCount]; for (int i = 0; i < lMeshCount; i++) { // Ensure our meshes are positioned relative to the center Matrix4x4 lMatrix = rMaterialMeshes.Transform[i]; lMatrix.m03 -= lCenter.x; lMatrix.m13 -= lCenter.y; lMatrix.m23 -= lCenter.z; // Process sub meshes lMeshes[i].transform = lMatrix; lMeshes[i].mesh = rMaterialMeshes.Meshes[i]; } // Create the object that will represent the new mesh UnityEngine.GameObject lMergedObject = new UnityEngine.GameObject(); lMergedObject.name = "Merged Mesh:" + rMaterialMeshes.Meshes.Count; lMergedObject.transform.position = lCenter; // Combine the meshes in the new object MeshFilter lMergedMeshFilter = lMergedObject.AddComponent(typeof(MeshFilter)) as MeshFilter; lMergedMeshFilter.mesh = new Mesh(); lMergedMeshFilter.sharedMesh.name = "Merged Mesh of " + lMeshCount + " items"; lMergedMeshFilter.sharedMesh.CombineMeshes(lMeshes, true); // Generate UV2 for lightmapping Unwrapping.GenerateSecondaryUVSet(lMergedMeshFilter.sharedMesh); // Set the material(s) for the new object MeshRenderer lMergedMeshRenderer = lMergedObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer; lMergedMeshRenderer.sharedMaterial = rMaterialMeshes.Material; // Assign the parent of the first selected object's parent if (mMergedParent != null) { lMergedObject.transform.parent = mMergedParent.transform; } lMergedObject.isStatic = true; XOCp_OptimizerHandler xoh = XOGloble.Root.GetComponent <XOCp_OptimizerHandler>(); if (xoh != null) { xoh.nlst.Add(lMergedObject); } // Return the newly created game object return(lMergedObject); }