public override void OnInspectorGUI() { DrawDefaultInspector(); PlaneGenerator script = (PlaneGenerator)target; if (GUI.changed) { } if (GUILayout.Button("Generate Plane")) { script.GeneratePlane(); } if (GUILayout.Button("Destroy Planes")) { //foreach(GameObject go in GameObject.FindGameObjectsWithTag("root")) // GameObject.DestroyImmediate(go); } }
void GenerateWorld() { // Divide the big terrain in smaller parts. There is a limit of vertices per mesh int numGrids = TOTAL_GRID_SIZE / GRID_SIZE; gridLand = new GameObject[numGrids * numGrids]; gridWater = new GameObject[numGrids * numGrids]; for (int x = 0; x < numGrids; x++) { for (int y = 0; y < numGrids; y++) { int idx = x + y * numGrids; // Calculate the offsets int posX = x * (GRID_SIZE - 1); int posY = y * (GRID_SIZE - 1); // Delegate generation of planes to a static class Mesh mesh = PlaneGenerator.GeneratePlane(GRID_SIZE, TOTAL_GRID_SIZE, posX, posY).CreateMesh(); mesh.bounds = new Bounds(new Vector3(GRID_SIZE / 2, 0, GRID_SIZE / 2), new Vector3(GRID_SIZE, TERRAIN_HEIGHT * 2, GRID_SIZE)); gridLand[idx] = new GameObject("Grid Land " + idx.ToString()); gridLand[idx].AddComponent <MeshFilter>(); gridLand[idx].AddComponent <MeshRenderer>(); gridLand[idx].GetComponent <Renderer>().material = landMat; gridLand[idx].GetComponent <MeshFilter>().mesh = mesh; gridLand[idx].transform.localPosition = new Vector3(-TOTAL_GRID_SIZE / 2 + posX, 0, -TOTAL_GRID_SIZE / 2 + posY); gridWater[idx] = new GameObject("Grid Water " + idx.ToString()); gridWater[idx].AddComponent <MeshFilter>(); gridWater[idx].AddComponent <MeshRenderer>(); gridWater[idx].GetComponent <Renderer>().material = waterMat; gridWater[idx].GetComponent <MeshFilter>().mesh = mesh; gridWater[idx].transform.localPosition = new Vector3(-TOTAL_GRID_SIZE / 2 + posX, 0, -TOTAL_GRID_SIZE / 2 + posY); } } }