4 using System.Collections.Generic;
11 private const int DefaultRows = 3;
12 private const int DefaultColumns = 3;
14 [Tooltip(
"Number of rows in the grid.")]
15 public int Rows = DefaultRows;
17 [Tooltip(
"Number of columns in the grid.")]
18 public int Columns = DefaultColumns;
20 [Tooltip(
"Distance between objects in the grid.")]
21 public float ObjectSpacing = 1.0f;
23 [Tooltip(
"Array of object prefabs to instantiate on the grid.")]
26 [Tooltip(
"Indicates whether to generate grid on component start.")]
27 public bool GenerateOnStart =
true;
39 float startX = -0.5f * ObjectSpacing * (Rows - 1);
40 float startY = -0.5f * ObjectSpacing * (Columns - 1);
41 for (
int i = 0; i < Rows; i++)
43 for (
int j = 0; j < Columns; j++)
45 GameObject prefab = GetRandomPrefab();
46 Vector3 pos =
new Vector3(startX + i * ObjectSpacing, startY + j * ObjectSpacing, 0.0f);
47 GameObject go = Instantiate(prefab, pos, Quaternion.identity) as GameObject;
48 go.transform.SetParent(transform,
false);
53 private GameObject GetRandomPrefab()
55 if (ObjectPrefabs.Count > 0)
57 int index =
Random.Range(0, ObjectPrefabs.Count);
58 return ObjectPrefabs[index];