Understanding TileData

When you modifying an STETilemap by script, you will use the methods SetTileData and GetTileData. They have a parameter called tileData that is an unsigned integer (uint).

This unsigned integer is a raw data representation of a tilemap cell.

This data is divided in different attributes, each of one using different bits of the unsigned integer of 32bits:

  • TileId [16bits, from 0 to 15]: this is the id of the tile used to draw this cell.
  • BrushId [12bits, from 16 to 27]: this is used to update the tileId when the tilemap mesh is updated
  • Flags [4bits, 28, 29, 30 and 31]:
    • Updated flag [bit 28]: used to inform the brush and other subsystem when the cell has been updated. For example, in a random brush, it will avoid changing the tileId again after this flag is set the first time.
    • Rotate 90º [bit 29]: the cell tile is rotated 90º
    • Vertical Flip [bit 30]: the cell tile is flipped vertically
    • Horizontal Flip [bit 31]: the cell tile is flipped horizontally


You can see some examples managing a tile data in the tutorial: Set and Get Tile data in an STETilemap


Set and Get Tile data in an STETilemap from script

You can set or get tile data using the methods in STETilemap component: SetTileData, SetTile, GetTileData and GetTile.

The difference between Tile and TileData is, TileData will use a raw tile data stored in an unsigned int containing the tile flags (flip horizontal, flip vertical, rotate 90º), the brush Id and the tile Id. You can see more details in the tutorial Understanding TileData.

GetTile will return the Tile instance stored in the tileset used by the tilemap.

So, in order to call any of the previous methods, you need first to get an STETilemap reference.

Getting the STETilemap reference to work with

// Gets the gameObject with the name "tilemapName"
GameObject tilemapObj = GameObject.Find("tilemapName");
// Gets the STETilemap component from a gameObject
STETilemap tilemap = gameObject.GetComponent<STETilemap>();

Setting a tile at some position in the tilemap

The tilemaps are boundless and will increase the boundaries when necessary.

SetTile and SetTileData will set a tile at some position. There are two versions of this method:

One is using a Vector2 position. This will set a tile at the cell position under the local position relative to the tilemap.

You can use this, for example, to change a tile under the player position:

//playerTransform will be a reference to the player transform
tilemap.SetTileData(playerTransform.position, 45); //sets the tile with id 45 in the cell under the player position
//Use this if the tilemap transform has been changed its position, rotation or scale
tilemap.SetTileData( tilemap.transform.InverseTransformPoint(playerTransform.position), 45);


The other version is using two integers gridX and gridY to set or get the tile at an specific cell. GridX is also the column and GridY the row.

After modifying a tilemap, you need to call UpdateMesh (to mark it to be updated during the next update) or UpdateMeshImmediate, to update it immediately.

Here are some examples setting tiles:

//Set a tile at grid position (20, -5) with a tileId (-1) empty, with a brushId 5.

tilemap.SetTile(20, -5, -1, 5);

//The same using SetTileData, the brushId should be shifted 16bits to the left, because the 

//first 16 bits are used to store the tileId. In this case, tileId will be 0, but doesn't matter 

//because the brush will change the tile after updating the tilemap

tilemap.SetTileData(20, -5, (5 << 16));

//Set a tile at position (2f, 5f), not the grid position (2, 5), locally to the tilemap and apply the flags

tilemap.SetTile(new Vector2(2f, 5f), 8, -1, eTileFlags.FlipH | eTileFlags.FlipV);

//And using SetTileData...

tilemap.SetTileData(new Vector2(2f, 5f), (8<<16) | (uint)(eTileFlags.FlipH | eTileFlags.FlipV)<<28);

// Erase the tile at grid position (-4, 9), the tileData will be -1 or 0xFFFFFFFF
tilemap.Erase(-4, 9);

//Always remember to call this to update the tilemap mesh and see the changes


Getting a tile at some position in the tilemap

Now you know how to set a tile, get a tile works almost the same but returning the tile data at some (local or grid) position.

In this case, SetTile will return directly a reference to the Tile instance in the Tileset used by the tilemap. To take the brush you need to use GetTileData and the use the method tileset.FindBrush(brushId).

Let see some examples:

//Get the tileData at grid position -4, -8
uint tileData = tilemap.GetTileData(-4, -8);

// Get the tileId, the brushId and the flags from tileData
int tileId = Tileset.GetTileIdFromTileData(tileData);
int brushId = Tileset.GetBrushIdFromTileData(tileData);
bool flipHorizontal = (tileData & Tileset.k_TileFlag_FlipH) != 0;
bool flipVertical = (tileData & Tileset.k_TileFlag_FlipV) != 0;
bool rot90 = (tileData & Tileset.k_TileFlag_Rot90) != 0;
eTileFlags tileFlags = (eTileFlags)(Tileset.GetTileFlagsFromTileData(tileData) << 28);

// Get the object Tile or TilesetBrush
Tile tile = tilemap.GetTile(-4, -8); // directly from the tilemap
tile = tilemap.Tileset.GetTile(tileId); // from the tileset with the tileId
TilesetBrush brush = tilemap.Tileset.FindBrush(brushId);

Some uses of getting the Tile object would be to check if it has colliders (for path finding for example) or to get a custom parameter like typeOfMaterial or isWall.

// will be true if the tile is not null and has any collider set on it
bool hasCollider = tile != null && tile.collData.type != eTileCollider.None;
// will be -1 if the tile is null, or it will get the value in the parameter typeOfMaterial or -1 if the parameter is not set
int typeOfMaterial = tile == null ? -1 : tile.paramContainer.GetIntParam("typeOfMaterial", -1);
// will be false if the tile is null, or it will get the value in the parameter isWall or false if the parameter is not set
bool isWall = tile == null ? false : tile.paramContainer.GetBoolParam("isWall", false);

// When will the tile be null?

// It will be null if the tileId is -1 because the tile was not set or it was set to empty ex: tilemap.Erase(-4, -8)