- Home /
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?
$$anonymous$$aybe you can force the game to keep the button selected by coding some more logic, I'm not exactly sure on how you would do this at the moment, but put certain conditions only to de-select? e.g selecting another unit.
I tried adding an event trigger that would reselect the current button on a Deselect event but it throws an error: "Attempting to select $$anonymous$$oveButton (UnityEngine.GameObject) while already selecting an object." when I do that (presumably this is a mechanism to avoid infinite loops).
Similar to this question http://answers.unity3d.com/questions/947602/unity5-ui-how-to-trigger-button-click-event-while.html by the looks of it
Answer by demented_hedgehog · Jun 05, 2015 at 02:42 AM
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... http://answers.unity3d.com/questions/872667/how-to-make-a-buttons-transition-stay-in-pressed-c.html
Answer by Gilead7 · Oct 17, 2016 at 11:37 PM
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.
Answer by biruktes1 · Jan 21, 2021 at 10:59 AM
You can get creative with toggles and toggle group and sprites and an extra UI image element as the child of background. Example:
Your answer
Follow this Question
Related Questions
Disable and enable canvas group by pressing an UI button. (C#) 2 Answers
Can't interact with world space ui button when cursor mode is locked any solutions? 1 Answer
Graphics Raycaster - Click on overlapped buttons 1 Answer
Unity5 UI - How to trigger button click event while preventing menu item deselect event? 1 Answer