I’m in the starting point of making a turn-based strategy with grid movement, and to create a finite grid in the editor I decided to use a custom editor script. The script allows me to set a size for the grid, then when I press a button it creates an empty GameObject for each cell and makes them all a child of another empty GameObject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class InitializeGrid : EditorWindow
{
int cols = 8;
int rows = 8;
float cellSpacing = 1;
GameObject gridParent;
GridHandler handler;
List<List<Transform>> cells; // Create a list of lists of transforms (functionally the same as a 2d list), to describe the cells
[MenuItem("Window/Grid Maker")]
public static void ShowWindow()
{
GetWindow<InitializeGrid>("Grid Maker");
}
private void OnGUI()
{
GUILayout.Label("Create Grid", EditorStyles.boldLabel);
cols = EditorGUILayout.IntField("Columns", cols);
//cols = ;
rows = EditorGUILayout.IntField("Rows", rows);
//rows = ;
cellSpacing = EditorGUILayout.FloatField("Spacing", cellSpacing);
if (GUILayout.Button("Create Grid"))
{
if (cols > 0 && rows > 0)
{
CreateGrid();
}
}
if (GUILayout.Button("Something") && cells.Count > 0)
{
Debug.Log(cells.Count.ToString());
Debug.Log(cells[0].Count.ToString());
Debug.Log(handler.cells.Count.ToString());
Debug.Log(handler.cells[0].Count.ToString());
}
}
void CreateGrid()
{
gridParent = new GameObject();
handler = gridParent.AddComponent<GridHandler>();
//DontDestroyOnLoad(gridParent);
cells = new List<List<Transform>>(); // Create a list of lists of transforms (functionally the same as a 2d list), to describe the cells
gridParent.name = "Grid";
int loopCount = 0;
//NESTED FOR LOOPS GO!!
for (int i = 0; i < cols; i++)
{
cells.Add(new List<Transform>()); //The list of lists has no items on it at first. Each time we make a new column, we need to add it to the list (of lists).
//By counting how far along the first list we are, we get the column.
for (int j = 0; j < rows; j++)
{
GameObject cell = new GameObject(); //Create empty GameObject to represent the cell
cell.name = "X: " + i + " Y: " + j; //Name it in the heirarchy
cell.transform.position = new Vector2(i * cellSpacing, j * cellSpacing); //Place it where I want it
cell.transform.SetParent(gridParent.transform); //Make it a child of the "Grid" object
cells[i].Add(cell.transform); //Look a which list we're on (which represents the column). Add this new cell to one of the second layer of lists.
//The second layer of lists represents the row.
AddTileGraphic(cell);
//print(loopCount.ToString());
loopCount += 1;
}
}
handler.cells = new List<List<Transform>>(cells); //Transfer the 2D list data we just made to the GridHandler component we wadded earlier. BUG: This doesn't seem to be working...
}
void AddTileGraphic(GameObject tile)
{
tile.AddComponent<SpriteRenderer>();
}
}
The above code adds each cell to a 2D (nested) list for later reference (I’m no expert but I think storing the cell coordinates like this will be a big help going forward) as it is created, and then copies this list to the object designated as the parent of the grid. More specifically it copies it to a component on that object with the following (much briefer) code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class GridHandler : MonoBehaviour
{
[SerializeField]
public List<List<Transform>> cells = new List<List<Transform>>();
private void Start()
{
print(cells.Count.ToString());
print(cells[0].Count.ToString()); //BUG: Only sometimes reads correctly
}
}
This code has print statements to use for debugging, which tell me how many rows and columns are recognized by the class. However, when I click play, the print statement suggest that the list is empty (and that the nested list doesn’t exist at all). The most confusing thing about it is that it isn’t consistent, when I create the grid in the editor it runs the start function and the code prints the size of the grid just fine! I really would like some help in knowing how I can make my GridHandler class remember the cell coordinates when playing.