Getting currently-selected GridBrush tile from code?

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?

“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.

Interesting. How can you access the palette grid instance from code?

@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;

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.

@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.

Nice! I worked it out - had to call 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!

1 Like

@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.

1 Like

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.

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

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…

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.