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

Custom Tilemap Brush to make GameObjects with sprite based on Tilemap Palette selection

Discussion in '2D' started by arvzg, Jun 21, 2018.

  1. arvzg

    arvzg

    Joined:
    Jun 28, 2009
    Posts:
    619
    I'm trying to make a new custom brush similar to 2d-extras' GameObject brush, except it makes new GameObjects with a SpriteRenderer and assigns a sprite to it based on what tile you've got selected on the Tile Palette.

    I've been pouring through the scripting manuals trying to find the right classes and things to use but can't seem to figure it out.

    Firstly I can't figure out how to reference the TilePalette at all, there is a GridPalette class in the manual but it doesn't seem to be what I'm looking for.

    When I figure out how to get access to the TilePalette, then the tiles on a tile palette seems to be accessible through GridSelection:

    upload_2018-6-21_11-42-2.png

    But when you look at the manual for GridSelection, there is no Tile field to access

    Can someone point me in the right direction? Thanks!
     
  2. arvzg

    arvzg

    Joined:
    Jun 28, 2009
    Posts:
    619
    Downloaded Unity's C# source and after looking at GridBrush I've figured it out! The TilePalette itself is a Tilemap, so to get the current selected Tile in the Tile Palette, override Pick(), GetComponent the TileMap from brushTarget, get the TileBase using Tilemap.GetTile(), cast it to a Tile and then you can get the sprite. Pretty complicated but not too hard
     
    luispedrofonseca likes this.
  3. ColinBellino

    ColinBellino

    Joined:
    Mar 1, 2015
    Posts:
    4
    Hi arvzg.

    Would it be possible for you to share the code of the brush you did ? It's something i would like to do as well later down the line, and that would be pretty nice to see where you went with this.

    Cheers!
     
  4. BartGeest

    BartGeest

    Joined:
    Sep 13, 2018
    Posts:
    1
    I got it working too, here's the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. [CreateAssetMenu(fileName = "New Brush", menuName = "Brushes/DefaultTileBrush", order = 1)]
    5. [CustomGridBrush(false, true, false, "WhateverBrush")]
    6. public class FireEmblemBrush : GridBrush {
    7.  
    8.     public ScriptableTile _scriptableTile;
    9.     private Tile _castedTile;
    10.  
    11.     private Tilemap _palette;
    12.  
    13.     public override void Paint(GridLayout gridLayout, GameObject brushTarget, Vector3Int position)
    14.     {
    15.         /*for (int i = 0; i < cells.Length; i++)
    16.         {
    17.             cells[i].tile = _scriptableTile;
    18.         }*/
    19.         base.Paint(gridLayout, brushTarget, position);  
    20.     }
    21.  
    22.     public override void Pick(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, Vector3Int pivot)
    23.     {
    24.         _palette = brushTarget.GetComponent<Tilemap>();
    25.         _castedTile = _palette.GetTile(pivot) as Tile;
    26.         _scriptableTile.TileSprite = _castedTile.sprite;
    27.         base.Pick(gridLayout, brushTarget, position, pivot);
    28.     }
    29. }
    Only thing that i am trying to do here is get the sprite from Pick() which is succesfull, and paint with _scriptableTile that has the sprite. Does someone has a clue for me?
     
    ColinBellino likes this.