I'm building a 2D strategy game where I've decided to use TileMap for spaces which characters can traverse. My tiles need to be prefabs, because they will have a script attached to them to store things like their type, cost, any effects they might have on them, name, etc.
To build the TileMap I imported Unity 2D Extras and used its prefab brush. I've actually tried this a couple of ways. First I just manually placed the prefabs and dragged them into the hierarchy under GameTileMap. When that didn't work I thought maybe I needed to do it with the prefab brush so I deleted everything and did the whole thing again that way. Still nothing.
Here is how my Hierarchy looks:
And this is the code I used to try and get that list of all the Tile prefabs I have there:
using UnityEngine.Tilemaps;
using UnityEngine;
public class TileManager : MonoBehaviour {
public static TileManager instance;
public Tilemap Tilemap;
void Awake() {
if (instance == null) {
instance = this;
}
else if (instance != this) {
Destroy(gameObject);
}
// Do I even have any tiles?
TileBase[] allTiles = Tilemap.GetTilesBlock(Tilemap.cellBounds);
// Print positions of each tile if I have any.
foreach (Vector3Int pos in Tilemap.cellBounds.allPositionsWithin) {
var localPlace = new Vector3Int(pos.x, pos.y, pos.z);
Debug.Log(localPlace);
}
}
}
The above code is on my TileManager. When run, allTiles
list is empty so of course no Debug output is logged.
Question:
Is it possible to get list of tiles that have been painted with the prefab brush?