How to detect painting/change to tilemap

I’d like to be able to execute a function when any of my tilemaps is painted on, but I’m not sure what the right way to do that is. Ideally I would be able to tell which tilemap was painted on, and what cell had changed. Should I be trying to achieve this with a custom brush, or some other way?

@theGreatSlutze

I guess you could first detect the object selection change in your editor script:

Then selection has activeObject which you can use to detect if you have a tilemap selected.

Then there is also Event for editor windows, so if selection gets changed, this method in window gets called.

And to get position, you can use Tilemap / Grid related helpers, something like this:

void GetSelectedTile()
{
    if (tilemap == null)
        return;

    if (Selection.activeObject != null)
    {
        var gridSel = GridSelection.active;

        if (gridSel)
        {
            pos = GridSelection.position;
            tile = tilemap.GetTile(pos.position);
        }
    }
}

Does the object selection change when you paint a tile? I wouldn’t have thought it would change since the tilemap is all a single object.

I guess probably not but you could figure it out.

When selection changes, I would check if and what tilemap I have or not.

When the grid selection is active, some tilemap must be selected, and grid selection also contains position, so you know what tile map is being painted, and you know the exact coordinate, then you can get the cell and its tile… you could track yourself if tile selection / cell has changed since last editor script update.