UI Elements
Radio Button Group
A simple button group controller
Overview
Radio Button Group manages a collection of Button components placed under the same parent and ensures that only one button is selected at a time. It automatically detects Button components in direct children, assigns index based listeners, updates their interaction states, and sends selection change events.
The group supports optional deselection, allowing all buttons to be unselected if the user clicks the selected one again.
Usage
Add Radio Button Group to a parent object
Place Button components as direct children
Clicking any button will:
- Deselect the previously selected button
- Select the new one using and make its state Selected
- Invoke OnSelectionChanged with the index
Properties
| Name | Type | Description |
|---|---|---|
selectedIndex | int | Currently selected button index, or -1 if none |
allowDeselection | bool | Allows clicking the selected button again to clear selection |
onSelectionChanged | UnityEvent<int> | Fires when selection changes (or -1 when cleared) |
Public Methods
| Name | Parameters | Description |
|---|---|---|
Initialize() | None | Rebuilds button references and listeners |
SetButton(index) | int | Selects the button at index |
DeselectAll() | None | Clears the current selection |
IsButtonSelected(index) | int | Returns true if index is selected |
SetInteractable(value) | bool | Enables or disables all buttons |
Code Example
using UnityEngine;
using Evo.UI;
public class RadioButtonGroupExample : MonoBehaviour
{
public RadioButtonGroup group;
void Start()
{
// Listen to change
group.onSelectionChanged.AddListener(index =>
{
Debug.Log("Selected index: " + index);
});
// Select button manually
group.SetButton(1);
// Deselect all
group.DeselectAll();
// Disable whole group
group.SetInteractable(false);
}
}