Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Tilemaps] having trouble with GetTileData

Discussion in '2D' started by PrimeCreator, Oct 14, 2018.

  1. PrimeCreator

    PrimeCreator

    Joined:
    Jul 17, 2017
    Posts:
    9
    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.

    Code (CSharp):
    1.     void Paintbrush(){
    2.  
    3.         Vector3 mousePos = Vector3.zero;
    4.         Vector3Int mousePosTranslated = Vector3Int.zero;
    5.  
    6.         if(toolMode == true){
    7.  
    8.             if(Input.GetMouseButtonDown(0)){
    9.  
    10.                 //World Space
    11.                 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    12.  
    13.                 //Mouse Position On tile Map
    14.                 //Converts worlds space click point to tile map click point.
    15.                 mousePosTranslated = activeTileMap.WorldToCell(mousePos);
    16.  
    17.                 //PAINTS BOXES
    18.                 //activeTileMap.BoxFill(mousePosTranslated, gameManager.cobbleTile, mousePosTranslated.x,mousePosTranslated.y,mousePosTranslated.x +1,mousePosTranslated.y +1);
    19.  
    20.                 //GETS TILE NAME AND PRINTS IT
    21. //                Debug.Log( activeTileMap.GetTile(mousePosTranslated));
    22. //                Debug.Log( gameManager.objectsMap.GetTile(mousePosTranslated));
    23. //                Debug.Log( gameManager.bordersMap.GetTile(mousePosTranslated));
    24.  
    25.  
    26.                 //now we can change tiles! :)
    27.  
    28.                 TileBase clickedTile = null;
    29.                 ITilemap itilemap = null;
    30.                 TileData tileData = null;
    31.  
    32.  
    33.                 clickedTile = activeTileMap.GetTile(mousePosTranslated);
    34.  
    35.                 //CHANGES TILES THAT ARE CLICKED ON
    36. //                if(clickedTile == null)
    37. //                    activeTileMap.SetTile(mousePosTranslated, gameManager.cobbleTile);
    38. //                else
    39. //                    Debug.Log("Obstructed");
    40.  
    41.                 GetTileData(mousePosTranslated, itilemap, ref tileData);
    42.  
    43.             }
    44.         }
    45.     }
     
    Alexandros-A likes this.
  2. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    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.
     
    PrimeCreator likes this.
  3. PrimeCreator

    PrimeCreator

    Joined:
    Jul 17, 2017
    Posts:
    9
    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.
     
    mvinc006 likes this.
  4. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    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.
     
  5. RandomiaGaming

    RandomiaGaming

    Joined:
    Apr 3, 2019
    Posts:
    12
    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?
     
  6. mvinc006

    mvinc006

    Joined:
    Jan 1, 2018
    Posts:
    91
    From memory, you pass the tilemap you are using, and the particular tile in that tilemap, as an argument into TileBase.GetTileData
     
  7. ehftwelve

    ehftwelve

    Joined:
    Aug 5, 2019
    Posts:
    1
    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
     
    CheekySparrow78 likes this.
  8. Goty-Metal

    Goty-Metal

    Joined:
    Apr 4, 2020
    Posts:
    168
    Sorry for reopening the thread but:
    Code (CSharp):
    1. clickedTile = activeTileMap.GetTile<Tile>(mousePosTranslated);
    2.  
    3. Always returns null, even if i'm sure the position has tile and even adding the position manually :/
     
  9. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    The tile you're getting may not be the <DesiredType>. 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.
     
  10. Goty-Metal

    Goty-Metal

    Joined:
    Apr 4, 2020
    Posts:
    168
    I can get the TileBase with GetTile(mousePosTranslated) but not the Tile, i'm using a standard Tile, nothing special, Unity default Tile.

    Thanks!
     
  11. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    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<Tile>() was unable to retrieve the Tile if the type is correct.
     
  12. a_karpins

    a_karpins

    Joined:
    Mar 8, 2023
    Posts:
    9
    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.

    Code (CSharp):
    1. Tilemap tilemap;
    2. private TileData tileData;
    3.  
    4. public void GetDatDataDaddy()
    5. {
    6.     tilemap = GameObject.Find("Grid").transform.GetChild(0).GetComponent<Tilemap>();//Grab your tilemap containing the tiles here
    7.     for (int x = tilemap.cellBounds.xMin; x < tilemap.cellBounds.xMax; x++)
    8.     {
    9.         for (int y = tilemap.cellBounds.yMin; y < tilemap.cellBounds.yMax; y++)
    10.         {
    11.             for (int z = tilemap.cellBounds.zMin; z < tilemap.cellBounds.zMax; z++)
    12.             {
    13.                 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
    14.                 if (curTile != null)
    15.                 {
    16.                     //Debug.Log(curTile.GetType());
    17.                     curTile.GetTileData(new Vector3Int(x, y, z), tilemap, ref tileData); //now tileData holds the information for curTile, supplying sprite info, etc
    18.                 }
    19.             }
    20.          
    21.         }
    22.  
    23.     }
    24. }