using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class ChunkManager : MonoBehaviour { private List activeChunks = new List(); private Camera viewer; [SerializeField] private MeshGeneration _meshGeneration; public Material material; public int viewableChunks = 3; public int chunkSize = 20; public float isoLevel = 0.4f; public InputAction action; // Start is called before the first frame update void Start() { viewer = Camera.main; action.Enable(); action.performed += _ => Generate(); Generate(); } private void Generate() { foreach (var c in activeChunks) { Destroy(c); } activeChunks.Clear(); for (int i = 0; i < viewableChunks; i++) { for (int j = 0; j < viewableChunks; j++) { for (int k = 0; k < viewableChunks; k++) { CreateChunks(new Vector3(i, j, k) * (chunkSize - 1)); } } } } // Update is called once per frame void Update() { } void CreateChunks(Vector3 offset) { var mesh = _meshGeneration.March(chunkSize, offset, isoLevel); GameObject chunkContainer = new GameObject($"chunk {offset.x} {offset.y} {offset.z}"); var chunk = chunkContainer.AddComponent(); chunk.Init(offset, mesh, material); activeChunks.Add(chunk); } }