- Home /
Help with array/lists manipulation in Editor script
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[i][j] != null)
{
mapArrayX[i][j].renderer.material.color = Color.red;
}
}
}
}
}
}
Any help?
sorry, the link to the picture is here: http://postimage.org/image/306v7glyc/
Answer by Owen-Reynolds · Sep 12, 2011 at 07:55 PM
Helpful if we knew what was wrong with the code you have, but... you have this line just sitting in OnGUI, running every frame:
mapArray = new GameObject[mapX, mapY];
That creates a fresh, empty map (contents are all null) each frame, erasing the old one. I'm thinking you intended to put the mapArray=
line inside the "Create New Map" button.
Design-wise, are you wanting to edit a persistant array of cubes? In that case, mapArray
should be in one script attached to a gameObject, and the editor should target it. Things declared in the editor are to help you edit, but aren't part of the game. If the cubes are just a visual aid to help you edit something else, than go ahead and leave them declared in the editor script.