24 lines
905 B
C#
24 lines
905 B
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(MeshFilter))]
|
|
[RequireComponent(typeof(MeshRenderer))]
|
|
public class MeshCombiner : MonoBehaviour
|
|
{
|
|
public static void Combine(Transform obj)
|
|
{
|
|
MeshFilter[] componentsInChildren = obj.GetComponentsInChildren<MeshFilter>();
|
|
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<MeshFilter>().sharedMesh = new Mesh();
|
|
obj.GetComponent<MeshFilter>().sharedMesh.name = "TerrainMesh";
|
|
obj.GetComponent<MeshFilter>().sharedMesh.CombineMeshes(array);
|
|
obj.gameObject.active = true;
|
|
obj.gameObject.renderer.enabled = false;
|
|
obj.gameObject.isStatic = true;
|
|
}
|
|
}
|