unity tilemap get tile position code example

Example 1: unity get coordinate of tile on tilemap

public class TilemapController2 : MonoBehaviour {     
   public Tilemap Tilemap;      // Use this for initialization     
   void Start () {         
     Vector3 tilePosition;         
     Vector3Int coordinate = new Vector3Int(0, 0, 0);         
     for (int i = 0; i < Tilemap.size.x; i++) {             
       for (int j = 0; j < Tilemap.size.y; j++) {                 
         coordinate.x = i; coordinate.y = j;                 
         tilePosition = Tilemap.CellToWorld(coordinate);                 
         Debug.Log(string.Format("Position of tile [{0}, {1}] = ({2}, {3})", coordinate.x, coordinate.y, tilePosition.x, tilePosition.y));             
       }         
     }     
   } 
 }

Example 2: unity tilemap get all tiles

public static class TilemapExtensions
{
    public static T[] GetTiles<T>(this Tilemap tilemap) where T : TileBase
    {
        List<T> tiles = new List<T>();
        
        for (int y = tilemap.origin.y; y < (tilemap.origin.y + tilemap.size.y); y++)
        {
            for (int x = tilemap.origin.x; x < (tilemap.origin.x + tilemap.size.x); x++)
            {
                T tile = tilemap.GetTile<T>(new Vector3Int(x, y, 0));
                if (tile != null)
                {
                    tiles.Add(tile);
                }
            }
        }
        return tiles.ToArray();
    }
}