Toggle Group for Buttons?

I’m trying to get toggle-group-like behaviour for a set of buttons.

I can do a certain amount of this in code, there’s a state machine behind the buttons and the button states reflect the available actions and the currently selected action for a unit. (Most of the time there’ll be a current action, the only time there won’t be is when the unit has no actions remaining… I think that’s an unimportant detail as far as this question goes).

The problem occurs when I click somewhere else on the screen and the button gets deselected. Presumably, the UI is deselecting the buttons. How can I stop this?

I’ll answer this myself for future readers.

I think I’m trying to do the wrong thing. It doesn’t make sense to require the buttons to be selected since that would stop the rest of the UI from working.

Instead I want the button to stay in the pressed state. Unfortunately it doesn’t do that so instead you need to roll your own ToggleButton that is a Toggle that looks like a button.

e.g. see here…

A quick and dirty way to do it, if you still have the question is to make two separate canvases. When you click the button, you enable one canvas or the other. Might not quite be what you are looking for, but another possibility.

You can get creative with toggles and toggle group and sprites and an extra UI image element as the child of background.
Example: