bad-piggies-0.1.3-porting/Assets/Scripts/Assembly-CSharp/MeshCombiner.cs

25 lines
905 B
C#
Raw Normal View History

2024-02-24 22:40:44 -05:00
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;
}
}