- Home /
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?
Answer by Pepperized · Apr 04, 2019 at 08:22 PM
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. https://github.com/Unity-Technologies/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.
Answer by donutLaserDev · Jul 10, 2018 at 06:40 AM
Take a look at this: https://docs.unity3d.com/ScriptReference/GridBrush.html
Answer by beanie4now · Jan 17 at 10:22 PM
For clarity: There are 2 steps.
Brush.Paint(targetTilemap.GetComponentInParent<Grid>(), targetTilemap.gameObject, cell);
Brush.SetGameObject(new Vector3Int(0,0,0),currentPrefab);
Initialize brush.
Instantiate GameObject