[Tilemaps] having trouble with GetTileData

Hi,

I am working with the tile maps and wanted to get the tile data of a tile that i click on with my mouse. I already completed the clicking on the tile part of the logic, but i am stuck when trying to use the GetTileData() method.

Any help guys?

P.S the docs were no help.

    void Paintbrush(){

        Vector3 mousePos = Vector3.zero;
        Vector3Int mousePosTranslated = Vector3Int.zero;

        if(toolMode == true){

            if(Input.GetMouseButtonDown(0)){

                //World Space
                mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                //Mouse Position On tile Map
                //Converts worlds space click point to tile map click point.
                mousePosTranslated = activeTileMap.WorldToCell(mousePos);

                //PAINTS BOXES
                //activeTileMap.BoxFill(mousePosTranslated, gameManager.cobbleTile, mousePosTranslated.x,mousePosTranslated.y,mousePosTranslated.x +1,mousePosTranslated.y +1);

                //GETS TILE NAME AND PRINTS IT
//                Debug.Log( activeTileMap.GetTile(mousePosTranslated));
//                Debug.Log( gameManager.objectsMap.GetTile(mousePosTranslated));
//                Debug.Log( gameManager.bordersMap.GetTile(mousePosTranslated));


                //now we can change tiles! :)

                TileBase clickedTile = null;
                ITilemap itilemap = null;
                TileData tileData = null;


                clickedTile = activeTileMap.GetTile(mousePosTranslated);

                //CHANGES TILES THAT ARE CLICKED ON
//                if(clickedTile == null)
//                    activeTileMap.SetTile(mousePosTranslated, gameManager.cobbleTile);
//                else
//                    Debug.Log("Obstructed");

                GetTileData(mousePosTranslated, itilemap, ref tileData);

            }
        }
    }
1 Like

Sorry if this is obvious but looking at your code are you elaborate what data your trying to get by using GetTileData?

I’ve done a lot of 2D programming and never needed to rely on this function, even looking at the documentation on it seems a bit meh.

1 Like

Thanks for the reply.

I have looked into it and realized that it only retrieves the rendering data therefore do not need to use it.

1 Like

No worries. I played around with your example a bit and found that it’s used to over ride the command structure (for example to enable custom animation). I’ll link an example later, on my phone atm.

I am trying to retrieve the sprite and collider type from a TileBase and would like to use TileBase.GetTileData however i only have a Tilemap not a ITilemap so what should i do and can i convert?

From memory, you pass the tilemap you are using, and the particular tile in that tilemap, as an argument into TileBase.GetTileData

Instead of:

clickedTile = activeTileMap.GetTile(mousePosTranslated);

use:

clickedTile = activeTileMap.GetTile<Tile>(mousePosTranslated);

And then set your clickedTile variable to type Tile instead of TileBase and you will have access to the sprite property

1 Like

Sorry for reopening the thread but:

clickedTile = activeTileMap.GetTile<Tile>(mousePosTranslated);

Always returns null, even if i'm sure the position has tile and even adding the position manually :/

The tile you’re getting may not be the . Make sure your tile derives from that type. Or you could have the wrong z, so your tile exists but on a different z-level.

I can get the TileBase with GetTile(mousePosTranslated) but not the Tile, i’m using a standard Tile, nothing special, Unity default Tile.

Thanks!

You could check the type of the TileBase that you retrieved from GetTile(mousePosTranslated) and see if it is a Tile? I would be strange if GetTile() was unable to retrieve the Tile if the type is correct.

Thanks, this helped me get to the solution. I had to create the variable curTile of type TileBase so I had access to the GetTile() command, and use it to get the type RuleTile at that location.

Tilemap tilemap;
private TileData tileData;

public void GetDatDataDaddy()
{
    tilemap = GameObject.Find("Grid").transform.GetChild(0).GetComponent<Tilemap>();//Grab your tilemap containing the tiles here
    for (int x = tilemap.cellBounds.xMin; x < tilemap.cellBounds.xMax; x++)
    {
        for (int y = tilemap.cellBounds.yMin; y < tilemap.cellBounds.yMax; y++)
        {
            for (int z = tilemap.cellBounds.zMin; z < tilemap.cellBounds.zMax; z++)
            {
                TileBase curTile = tilemap.GetTile<RuleTile>(new Vector3Int(x, y, z)); //Replacing <RuleTile> with  <TileBase> and removing comments on the Debug.Log helps find the type of tile you have at the location
                if (curTile != null)
                {
                    //Debug.Log(curTile.GetType());
                    curTile.GetTileData(new Vector3Int(x, y, z), tilemap, ref tileData); //now tileData holds the information for curTile, supplying sprite info, etc
                }
            }
          
        }

    }
}