- Home /
How to stack components on a Tile/Tilemap
I'm creating a Hex based grid game, I thought the Hex Tilemap would be an obvious fit for this.
Creating the initial grid was easy enough, but now I'm trying to stack things on top of individual tiles, and being pretty new to Unity, I'm finding this difficult. I've seen lots of tutorials for how to simply draw a Tilemap/grid, or lots of tutorials using the Palette, but I'd like to do this entirely through code.
My current solution is to pass in two separate Tilemaps, belonging to the same grid. I use the bottom tilemap (named Floor) for placing the floor/terrain tiles. On the second Tilemap I'm trying to place some icons or text as a status type layer. These will exist on every single tile.
// This is a grid I've passed through into the Script from the Unity IDE
public Grid grid;
// These two Tilemaps also exist in the editor as children to the Grid and are also passed in.
public Tilemap floor;
public Tilemap statusOverlay;
// Start is called before the first frame update
void Start()
{
// LoadSprites loads the floor and status tiles into a Sprite[], it works, so is not totally relevant to this question.
LoadSprites();
for (var xd = 0; xd <= 10; xd++)
{
for (var yd = 0; yd <= 10; yd++)
{
var currentPos = new Vector3Int(xd, yd, 0);
// Set tile type/floor image
var floorTile = ScriptableObject.CreateInstance<Tile>();
int floorSpriteIndex = Roll(floorSprites.Length)-1;
floorTile.sprite = floorSprites[floorSpriteIndex];
floor.SetTile(currentPos, floorTile);
// Add another tile which should just add a simple icon for now.
var statusOverlayTile = ScriptableObject.CreateInstance<Tile>();
statusOverlayTile.sprite = statusSprites[1];
statusOverlay.SetTile(currentPos, statusOverlayTile);
}
}
}
In the future I will need to add more items on top of the same grid, e.g. Units, maybe buildings, etc.. These will not be on every Tile but a subset of the Tiles.
Is this current approach good, performance wise?
I think it potentially makes sense to have a Tilemap for the Floor and Players, but maybe not so much for the Status tilemap, which will have a Tile for every single Floor tile. For this it would be good if I could either directly draw the status icons/text onto the floor Tiles, dynamically. Or create some sort of grid floor component which both draws the Floor, the status affects, and anything else in the future that might belong to a Tile itself.
Or is it not much of a performance issue to create/maintain double the amount of tiles, let's say if I scale up to thousands of tiles in the future.
FYI I think this is on unity 4.5, according to Unity > Help > About Unity
Your answer
Follow this Question
Related Questions
Grid and or Tile System 0 Answers
How can I compare two tilemaps by color? 1 Answer
How can i make the Unity2d grid use rounded numbers? 1 Answer
How do you find adjacent tiles of the same type in grid array [x,y].type == [x+1,y].type 0 Answers
How can I access to a tile properties in a Tilemap? 0 Answers