I’m trying to make a procedurally generated tiled map/object set. I have each square in the map (int[,]
) store an index of the type of tile it is. Then, I want to be able to use this to randomly select from a set of prefabs to spawn the actual tile with Instantiate(). Furthermore, I want different types of tiles to have different types of these variants (e.g. a “grass” tile may have 2 or 3 variants, whereas a “house” tile would have 7-10 variants, each with its own model/child objects/particle systems).
Optimally, I’d like to have a GameObject[][]
, with the outer index being the tile type, and the inner index being randomly selected, like so (extra variables for clarity):
public GameObject[][] tilePrefabs;
void generateMap(int[,] map) {
System.Random r = new System.Random(seed);
//for every tile (loop through x and y):
int tileType = map[x,y];
int numVariants = tilePrefabs[tileType].length;
GameObject tileToAdd =
Instantiate(tilePregabs[tileType][r.Next(0,numVariants)]);
}
Problem is, I can’t figure out a way to assign prefabs to either a GameObect[][]
or a GameObject[,]
through the editor. Is there a way around this, besides doing something like
GameObject[] tileType1;
GameObject[] tileType2;
...
and so on?
Thanks!
tl;dr: Any way to assign prefabs to an array of arrays or 2D array, preferably in the editor?