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

Get Selected TileBase from Tile Palette Window

Discussion in '2D' started by TRRBusiness, Dec 9, 2020.

  1. TRRBusiness

    TRRBusiness

    Joined:
    Aug 7, 2019
    Posts:
    6
    So, not sure why this is so hard, or maybe I'm just dumb but, this is the only way I've found to get the current TileBase selected on the Tile Palette window. If there is an easier way, I'd like to know. For context, I have a tilemap solution that is custom, but I'm still going to use a Tile Palette to paint maps since none of my paint tiles relate to actual tile data (auto-tiling) and I figured it would be easier.

    I googled and searched for a bit and only found one solution that halfway worked.
    https://forum.unity.com/threads/cus...te-based-on-tilemap-palette-selection.537109/
    thread by user https://forum.unity.com/members/arvzg.8369/
    code modified from user https://forum.unity.com/members/bartgeest.2023870/

    It took me so long to find that it might be worth re-posting similar (simplified) information with a clearer thread title name for people searching. Not sure if that is appropriate or not.

    Cheers.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor.Tilemaps;
    5. using UnityEngine;
    6. using UnityEngine.Tilemaps;
    7.  
    8. [CreateAssetMenu(fileName = "SomeBrush", menuName = "Brushes/SomeBrush", order = 1)]
    9. [CustomGridBrush(false, true, false, "SomeBrush")]
    10. public class QuadBrush : GridBrush {
    11.  
    12.     public override void Pick(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, Vector3Int pickStart) {
    13.         Tilemap palette = brushTarget.GetComponent<Tilemap>();
    14.  
    15.         if (palette != null) {
    16.             TileBase tile = palette.GetTile(new Vector3Int(position.x, position.y, 0));
    17.  
    18.             if (tile != null) {
    19.                 Debug.Log("Brush Picked: " + tile.name);
    20.             }
    21.         }
    22.  
    23.         base.Pick(gridLayout, brushTarget, position, pivot);
    24.     }
    25. }
    26.  
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    If you are using the default Brush, you can get it with:

    Code (CSharp):
    1. (GridPaintingState.gridBrush as GridBrush).cells[0].tile;
    or :

    upload_2020-12-9_16-6-20.png
     
  3. TRRBusiness

    TRRBusiness

    Joined:
    Aug 7, 2019
    Posts:
    6
    I have no clue how I missed this. I grabbed the GridBrushBase and just stared at it sideways for awhile.

    Side note, I'm finding that the custom brush is working out since I can effectively disable all the functions of the Tile Palette Window except what I want, which in my oddball case is a good thing.

    Thanks for the response!

    R
     
  4. Kellyrayj

    Kellyrayj

    Joined:
    Aug 29, 2011
    Posts:
    933
    Is there an easy way to do this if you are not using the default brush? I'm working on a solution for the fill box Brush for example
     
  5. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    This would depend on your implementation of your brush, as the Pick function may not be the same.

    You should be able to query the state of your brush from
    GridPaintingState.gridBrush
    , which should let you find out the contents of the brush?