Hi,
I’m trying to create an arbitrary 2D grid of cubes in an Editor script, and then select random portions of this grid, just like in this picture:
I’ve tried using a 2D array, but it won’t work unless I define its dimensions while declaring it (and it’s not what I need, because I need the user to set them). Here’s the code using arrays:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class RNDGEN : EditorWindow
{
private int mapX, mapY = 0;
GameObject[,] mapArray;
GameObject floorTile;
GameObject baseGrid = null;
[MenuItem ("Window/My Window")]
static void Init ()
{
// Get existing open window or if none, make a new one:
RNDGEN rndGen = (RNDGEN)EditorWindow.GetWindow (typeof (RNDGEN));
}
void OnGUI()
{
mapX = EditorGUI.IntSlider(new Rect(3, 3, position.width - 6, 15), "Map width:", mapX, 2, 50);
mapY = EditorGUI.IntSlider(new Rect(3, 25, position.width - 6, 15), "Map height:", mapY, 2, 50);
mapArray = new GameObject[mapX, mapY];
floorTile = (GameObject)Resources.Load("Floor_Tile");
if (GUI.Button(new Rect(3, 45, position.width - 6, 20), "Create Grid"))
{
baseGrid = new GameObject();
baseGrid.name = "Base_Grid";
for (int i = 0; i < mapX; i++)
{
for (int j = 0; j < mapY; j++)
{
GameObject tile = (GameObject)Instantiate(floorTile, new Vector3(i, 0, j), Quaternion.identity);
tile.transform.parent = baseGrid.transform;
tile.name = "Tile_" + i.ToString() + "-" + j.ToString();
mapArray[i, j] = tile;
}
}
}
if (GUI.Button(new Rect(3, 75, position.width - 6, 20), "Test"))
{
for (int i = 0; i < Random.Range(0, mapArray.GetLength(0)); i++)
{
for (int j = 0; j < mapArray.GetLength(1); j++)
{
if (mapArray[i, j] != null)
{
mapArray[i, j].renderer.material.color = Color.red;
}
}
}
}
}
}
Then I tried using nested lists, but it’s not working like it should. Maybe I’m doing something wrong while iterating through the nested lists. Here’s the code using lists:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class RNDGEN : EditorWindow
{
private int mapX, mapY = 0;
List<List<GameObject>> mapArrayX = new List<List<GameObject>>();
List<GameObject> mapArrayY = new List<GameObject>();
GameObject floorTile;
GameObject baseGrid = null;
[MenuItem ("Window/My Window")]
static void Init ()
{
// Get existing open window or if none, make a new one:
RNDGEN rndGen = (RNDGEN)EditorWindow.GetWindow (typeof (RNDGEN));
}
void OnGUI()
{
mapX = EditorGUI.IntSlider(new Rect(3, 3, position.width - 6, 15), "Map width:", mapX, 2, 50);
mapY = EditorGUI.IntSlider(new Rect(3, 25, position.width - 6, 15), "Map height:", mapY, 2, 50);
floorTile = (GameObject)Resources.Load("Floor_Tile");
if (GUI.Button(new Rect(3, 45, position.width - 6, 20), "Create Grid"))
{
baseGrid = new GameObject();
baseGrid.name = "Base_Grid";
for (int i = 0; i < mapX; i++)
{
for (int j = 0; j < mapY; j++)
{
GameObject tile = (GameObject)Instantiate(floorTile, new Vector3(i, 0, j), Quaternion.identity);
tile.transform.parent = baseGrid.transform;
tile.name = "Tile_" + i.ToString() + "-" + j.ToString();
mapArrayY.Add(tile);
mapArrayX.Add(mapArrayY);
}
}
}
if (GUI.Button(new Rect(3, 75, position.width - 6, 20), "Test"))
{
for (int i = 0; i < Random.Range(0, mapArrayX.Count); i++)
{
for (int j = 0; j < mapArrayY.Count; j++)
{
if (mapArrayX*[j] != null)*
-
{*
_ mapArrayX*[j].renderer.material.color = Color.red;*_
* }*
* }*
* }*
* }*
* }*
}
Any help?