using UnityEngine;using System.Collections;using System.Collections.Generic; public class CombineMeshes : MonoBehaviour{ void Start() { //获取MeshRender; MeshRenderer[] meshRenders = GetComponentsInChildren(); //材质; Material[] mats = new Material[meshRenders.Length]; for (int i = 0; i < meshRenders.Length; i++) { mats[i] = meshRenders[i].sharedMaterial; } //合并Mesh; MeshFilter[] meshFilters = GetComponentsInChildren(); 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; meshFilters[i].gameObject.SetActive(false); } MeshRenderer mr = gameObject.AddComponent(); MeshFilter mf = gameObject.AddComponent();
mf.mesh = new Mesh();
mf.mesh.CombineMeshes(combine, false);
gameObject.SetActive(true);
mr.sharedMaterials = mats;
}
网友评论