How can I use the GridBrush in Unity at runtime in the game?

I want to use the brush to paint tiles in the grid while the game is running, so the player could actually paint a map. How can I use the brush feature at runtime?

I’m not an expert but I did managed to get it working

First make your GridBrush, I used a PrefabBrush from the 2D extras.

Then I tried a few things and found this to work.


brush.Paint(grid, tilemap.gameObject, vector3IntCellLocation);

Where grid is the Tilemap Grid, tilemap is the Tilemap, and vector3IntCellLocation is the location to paint in Cell coordinates. You can use grid.WorldToCell(Vector3 position) to get the Cell coordinate.


Using this, it will add prefabs into the scene with parent being the Tilemap, rather than in the Tilemap. It won’t let you place more than one prefab at the same location so it’s quite convenient.

Take a look at this: https://docs.unity3d.com/ScriptReference/GridBrush.html

For clarity: There are 2 steps.

Brush.Paint(targetTilemap.GetComponentInParent<Grid>(), targetTilemap.gameObject, cell);
Brush.SetGameObject(new Vector3Int(0,0,0),currentPrefab);
  1. Initialize brush.
  2. Instantiate GameObject