Using Prefab Brushes in Code

I’m working on a 2D top down game and was thinking of generating things like trees, etc with prefab brushes.
The reason for this is the rendering. So in theory I could ensure that everything that should be in front will be rendered that way.

I can’t find any resources on how to apply the prefab brushes in the code and ended up trying this:

    public PrefabBrush prefabBrush;
    private void GeneratePrefabBrushProps()
    {
        Tilemap prefabBrushTilemap = CreateTilemap("Prefab Brush Test", 2, 0);
        prefabBrushTilemap.SetTile(new Vector3Int(40, 40, 0), prefabBrush);
    }

Does anyone know a way to insert the prefab brushes into a tilemap via code like normal tiles?

If what you mean by “brush” is the type of brush that’s used with the Tilemap Palette then the answer is: Palette brushes are editor only, they can’t be used in a built game or app.

If you mean something else please be more specific. It looks like you’re trying to paint a brush onto a Tilemap as a tile which isn’t possible. Doesn’t your code editor complain about a type mismatch or??

1 Like

Yea so the brush is from 2D.extras and supposed to be used in the TilePallete.
I don’t assume that it is possible to use them in runtime code.

Brushes are editor-only, and in any case, is not a tile so there is no way to use it as a tile. Sorry. If you want your prefab in front of something use layers, rendering order settings, something like that.