Toggle Group in UI Toolkit

Hello, I was trying out to create a simple UI, which contain a simple form. In this form I wanted to have some text input field and toggles. The problem I have right now is that I can’t find any simple way to group my toggles. I would like to have only one enabled (or set to true) toggle in the group. Is such a feature built in the UI Toolkit?

Screenshot of simple UI for reference:

Like in the attached screenshot, users should be able to choose only 1 race and 1 class.
Thanks for your help!

Hello, we have this type of control coming but I can’t say the exact version yet - coming soon, though!

Hey, what’s the status on UI Toolkit toggle groups? Or the same functionality as GUI.Toolbar from the previous system? Something like this:

6830780--794093--upload_2021-2-12_10-53-53.png

Should we just implement them as buttons in a horizontal layout and manage the checked state with styling or is there a built-in control that ensures the editor styling is consistent?

1 Like

Can this be used?

RadioButton and GroupBox are out on 2021.1 already!

For Toolbars you can use separate ToolbarToggle instances to have that behavior.

1 Like

So for 2021.1, there still isn’t a replacement for the GUI.Toolbar as UI Toolkit control?
I’ve already asked this a long time ago, which if I remember correctly, was also replied with “not at the moment, use ToolbarToggle”.

So now theres a GroupBox which is a foundation for RadioButtonGroup, but it doesn’t manage the ToolbarToggle because it doesn’t implement IGroupBoxOption right?
Are we supposed to inherit from ToolbarToggle and add IGroupBoxOption?

Edit: just noticed IGroupBoxOption is internal, so you cannot even use it

I completely misunderstood what the GUI.Toolbar does, sorry about that! :face_with_spiral_eyes:
But we indeed don’t have that same control on the UI Toolkit side, so you’d have to implement your own.

You have an example on the UI Builder, it’s called ButtonStrip. If you use the UI Builder from a package (with Unity 2020.3 or 2021.1 for example), you have easy access to the source code.

Hope that helps! :slight_smile:

1 Like

Ah right, I completely missed that one! haha, thanks for the tip I’ll have a look at it later.

1 Like

Why is ButtonStrip not available as a default control? It seems like something very commonly used.

Anyways, UI Toolkit source is not available on 2022.2.0b16.

EDIT: Found it!
https://github.com/Unity-Technologies/UnityCsReference/blob/2022.2/External/MirroredPackageSources/com.unity.ui.builder/Editor/Utilities/ButtonStrip/ButtonStrip.cs

1 Like

Sometimes some controls get implemented in a fashion that we are not comfortable making available to the public because we didn’t have the time to really validate the API and usability are the correct one, but we still need to have the component internally. That doesn’t mean it’ll never be out to public, just means the current implementation is not ready to be public yet! I can assure you that such a common use case is on our radar and there will be some default implementation at some point :wink:

Hello, thanks for the reply! I do hope there is also an alternate to RadioButtonGroup, which is BaseField, so we can have IMGUI GUILayout.Toolbar equivalent like this use case.

Maybe call is ToggleButtonGroup or something.

_selectedToolIndex.intValue = GUILayout.Toolbar(_selectedToolIndex.intValue, _toolNames, "Button");

Love the constructor, and its very handy!