Last active
December 14, 2015 15:09
-
-
Save hiroki-o/5106206 to your computer and use it in GitHub Desktop.
Combine all mesh of children into one mesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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