using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(MeshRenderer))] [RequireComponent(typeof(MeshFilter))] [ExecuteInEditMode] public class ProceduralMesh : MonoBehaviour { private Mesh _mesh; private void Awake() { } private void ValidateMeshCreated() { if(_mesh == null) { _mesh = new Mesh(); var filter = GetComponent(); filter.sharedMesh = _mesh; } } public void Rebuild(MeshData data, bool recalculateBounds = true) { ValidateMeshCreated(); _mesh.Clear(); _mesh.SetVertices(data.Vertices); _mesh.SetColors(data.Colors); for (int i = 0; i < data.Submeshes.Count; ++i) { var submesh = data.Submeshes[i]; _mesh.SetIndices(submesh.Indices, submesh.Topology, i, recalculateBounds, submesh.BaseVertex); } if (recalculateBounds) { _mesh.RecalculateBounds(); } } }