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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Getting currently-selected GridBrush tile from code?

Discussion in '2D' started by theGreatSlutze, Nov 21, 2019.

  1. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    I'd like to execute a function when the selected tile (the tile to be painted onto the tilemap) changes, but I can't find anything about how to know that information. Based on this API description, it used to be possible to access the selected tile via the GridBrush `cells` property, but that doesn't seem to exist anymore. How can I find what tile is currently selected?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "How can I find what tile is currently selected?"

    Tile Palette (Asset file) is actually a Grid, the same Unity Tilemap uses run-time.

    I've also figured out earlier that Palette Asset actually has a hidden sub asset, and it is Tilemap.

    So it is just an asset you can create yourself using editor scripting, only the display part in Tile Palette view is something different and you can't recreate it I guess (don't know might be wrong).

    So with Grid, you have access to Grid Selection. You can figure this out - when you do a selection with arrow tool in Tile Palette window you will see inspector showing GridSelection instance... which contains some info about user selection in Palette (= Grid).

    And from there you could get access to selection position, which in turn is the position in Tilemap. Then you could get access to info about which Tile asset is selected in your Palette's hidden Tilemap.
     
  3. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    Interesting. How can you access the palette grid instance from code?
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @theGreatSlutze

    Palette grid is an asset file, so yes.

    You have to create a Palette asset file to be able to see it in Tile Palette Editor.

    Edit.
    You don't actually even need grid that much (but it is needed to understand the concept).

    var gridSel = GridSelection.active;


    Will give you the info if you have a selection, but it is just a bool.

    So then you need the position:

    var pos = GridSelection.position;
     
    Last edited: Nov 21, 2019
  5. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    I still feel like I’m not understanding. Could you perhaps provide a code snippet showing how you’d access the currently selected brush tile?

    overall I’m still just confused about why this functionality, which seems to have existed before, isn’t around anymore, and if it’s moved to somewhere else.
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @theGreatSlutze

    I pretty much explained all the steps. You can do this both runtime and in editor scripts like Editor Window. I don't have time right now to create such a script... maybe later.
     
  7. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    Nice! I worked it out - had to call
    Code (CSharp):
    1. GetComponentInChildren<Tilemap>
    on the reference to my palette asset ( which I had to include as a grid) to grab the tilemap, but that’ll work. Thanks!
     
    eses likes this.
  8. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @theGreatSlutze

    Yeah, I don't think there is any other way to do that - as Palette is just an asset of type Grid, with a hidden child containing a Tilemap like I said earlier. So just grab the tilemap from the asset. I think I ran into this when I created automatic tile palette builder, which created tile palette from separate tiles or sprites.
     
    theGreatSlutze likes this.
  9. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    Update: this definitely works if you use the box-select on the tile palette. But, if you have the brush tool selected and click on a tile on the palette, it does not work, because that uses the eyedropper tool instead of the grid-select tool. Is there a way I can read that tool similarly? I almost never use the select tool to grab a tile to paint with.
     
  10. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    UGH feeling really dumb - I missed that I needed `UnityEditor.Tilemaps` - from there I can access the cells on the selected gridBrush, which seems to do what I need
     
  11. TKG_Studios

    TKG_Studios

    Joined:
    Nov 3, 2020
    Posts:
    10
    Hello GreatSultze

    I'm actually trying to figure out how to leverage this functionality to create a button on the Grid Selection inspector. Would you be able to help me out here? I can't seem to figure out where I need to call GetComponentInChildren.... I am essentially trying to create a customer editor script (typeof(GridSelection))...

    Can you help? This is the only thread I've found ever about this...
     
  12. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Hi, would it be possible to share how your would like to customise the inspector for the GridSelection?

    This may be strange, but the contents of the inspector of the GridSelection depends on the inspector of the currently active brush in the Tile Palette. The method GridBrushEditorBase.OnSelectionInspectorGUI is called by the inspector of the GridSelection. You can create a custom editor for the GridBrush overriding this to get the functionality you want.