Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Using Prefab Brushes in Code

Discussion in '2D' started by unity_rLrstvXScQEvow, Mar 27, 2023.

  1. unity_rLrstvXScQEvow

    unity_rLrstvXScQEvow

    Joined:
    Mar 4, 2019
    Posts:
    28
    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:
    Code (CSharp):
    1.     public PrefabBrush prefabBrush;
    2.     private void GeneratePrefabBrushProps()
    3.     {
    4.         Tilemap prefabBrushTilemap = CreateTilemap("Prefab Brush Test", 2, 0);
    5.         prefabBrushTilemap.SetTile(new Vector3Int(40, 40, 0), prefabBrush);
    6.     }
    Does anyone know a way to insert the prefab brushes into a tilemap via code like normal tiles?
     
  2. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    238
    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??
     
    unity_rLrstvXScQEvow likes this.
  3. unity_rLrstvXScQEvow

    unity_rLrstvXScQEvow

    Joined:
    Mar 4, 2019
    Posts:
    28
    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.

     
  4. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    238
    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.