Skip to content

Instantly share code, notes, and snippets.

@hiroki-o
Last active December 14, 2015 15:09
Show Gist options
  • Save hiroki-o/5106206 to your computer and use it in GitHub Desktop.
Save hiroki-o/5106206 to your computer and use it in GitHub Desktop.
Combine all mesh of children into one mesh
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class MeshCombiner : MonoBehaviour {
void Start() {
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>() as MeshFilter[];
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
for (int i = 0; i < meshFilters.Length; ++i) {
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
if( meshFilters[i].gameObject != gameObject ) {
meshFilters[i].gameObject.SetActive(false);
}
}
MeshFilter mf = GetComponent<MeshFilter>() as MeshFilter;
mf.mesh = new Mesh();
mf.mesh.CombineMeshes(combine);
mf.mesh.Optimize();
mf.mesh.RecalculateBounds();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment