using UnityEngine; [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] public class MeshCombiner : MonoBehaviour { public static void Combine(Transform obj) { MeshFilter[] componentsInChildren = obj.GetComponentsInChildren(); CombineInstance[] array = new CombineInstance[componentsInChildren.Length]; for (int i = 0; i < componentsInChildren.Length; i++) { array[i].mesh = componentsInChildren[i].sharedMesh; array[i].transform = componentsInChildren[i].transform.localToWorldMatrix; componentsInChildren[i].gameObject.isStatic = true; } obj.GetComponent().sharedMesh = new Mesh(); obj.GetComponent().sharedMesh.name = "TerrainMesh"; obj.GetComponent().sharedMesh.CombineMeshes(array); obj.gameObject.SetActive(true); obj.gameObject.GetComponent().enabled = false; obj.gameObject.isStatic = true; } }