|
| 1 | +# Extensions Toggle Group |
| 2 | + |
| 3 | +An enhanced ToggleGroup component that manages a group of ExtensionsToggle components, ensuring only one toggle is active at a time. |
| 4 | + |
| 5 | +--------- |
| 6 | + |
| 7 | +## Contents |
| 8 | + |
| 9 | +> 1 [Overview](#overview) |
| 10 | +> |
| 11 | +> 2 [Properties](#properties) |
| 12 | +> |
| 13 | +> 3 [Methods](#methods) |
| 14 | +> |
| 15 | +> 4 [Usage](#usage) |
| 16 | +> |
| 17 | +> 5 [Video Demo](#video-demo) |
| 18 | +> |
| 19 | +> 6 [See also](#see-also) |
| 20 | +> |
| 21 | +> 7 [Credits and Donation](#credits-and-donation) |
| 22 | +> |
| 23 | +> 8 [External links](#external-links) |
| 24 | +
|
| 25 | +--------- |
| 26 | + |
| 27 | +## Overview |
| 28 | + |
| 29 | +The Extensions Toggle Group is an enhanced version of Unity's standard ToggleGroup that manages ExtensionsToggle components. It ensures that only one toggle in the group can be on at a time (unless `AllowSwitchOff` is enabled) and provides events for monitoring group state changes. |
| 30 | + |
| 31 | +Key features include: |
| 32 | + |
| 33 | +- **Enforced Single Selection**: Only one toggle can be selected at a time |
| 34 | +- **Optional Multi-Off Mode**: When enabled, all toggles can be off |
| 35 | +- **Dual Event System**: Separate events for group state changes and individual toggle changes |
| 36 | +- **Active Toggles Enumeration**: Access all currently active toggles in the group |
| 37 | +- **Programmatic Control**: Methods to query and control toggle states |
| 38 | + |
| 39 | +--------- |
| 40 | + |
| 41 | +## Properties |
| 42 | + |
| 43 | +The properties of the Extensions Toggle Group control are as follows: |
| 44 | + |
| 45 | +Property | Description |
| 46 | +-|- |
| 47 | +*Allow Switch Off*|When true, allows all toggles in the group to be off. When false, at least one toggle must be on. |
| 48 | +***On Toggle Group Changed*** (event)|Invoked when any toggle in the group changes state, passes whether any toggles are on. |
| 49 | +***On Toggle Group Toggle Changed*** (event)|Invoked when a toggle in the group changes, passes the new boolean state. |
| 50 | + |
| 51 | +### Additional properties available in code |
| 52 | + |
| 53 | +Property | Return Type | Description |
| 54 | +-|-|- |
| 55 | +AllowSwitchOff|bool|Get or set whether all toggles can be off. |
| 56 | +SelectedToggle|ExtensionsToggle|Get the currently selected toggle, or null if none selected. |
| 57 | + |
| 58 | +--------- |
| 59 | + |
| 60 | +## Methods |
| 61 | + |
| 62 | +Method | Arguments | Description |
| 63 | +-|-|- |
| 64 | +*NotifyToggleOn*|toggle (ExtensionsToggle)|Internally called when a toggle becomes on. Turns off all other toggles in the group. |
| 65 | +*RegisterToggle*|toggle (ExtensionsToggle)|Register an ExtensionsToggle to be managed by this group. |
| 66 | +*UnregisterToggle*|toggle (ExtensionsToggle)|Unregister an ExtensionsToggle from this group. |
| 67 | +*AnyTogglesOn*|N/A|Returns true if at least one toggle in the group is on. |
| 68 | +*ActiveToggles*|N/A|Returns an enumerable collection of all toggles that are currently on. |
| 69 | +*SetAllTogglesOff*|N/A|Turns off all toggles in the group. |
| 70 | + |
| 71 | +--------- |
| 72 | + |
| 73 | +## Usage |
| 74 | + |
| 75 | +Add the Extensions Toggle Group component to a GameObject, typically the parent of the toggles: |
| 76 | + |
| 77 | +"Add Component -> UI -> Extensions -> Extensions Toggle Group" |
| 78 | + |
| 79 | +### Basic Group Setup |
| 80 | + |
| 81 | +```csharp |
| 82 | +public ExtensionsToggleGroup toggleGroup; |
| 83 | +public ExtensionsToggle[] toggles; |
| 84 | + |
| 85 | +void Start() |
| 86 | +{ |
| 87 | + // The toggles should have their Group property set to this group |
| 88 | + // This can be done in the inspector or programmatically: |
| 89 | + foreach (var toggle in toggles) |
| 90 | + { |
| 91 | + toggle.Group = toggleGroup; |
| 92 | + } |
| 93 | + |
| 94 | + // Listen to group changes |
| 95 | + toggleGroup.onToggleGroupChanged.AddListener(OnGroupChanged); |
| 96 | +} |
| 97 | + |
| 98 | +void OnGroupChanged(bool anyToggleOn) |
| 99 | +{ |
| 100 | + Debug.Log("Any toggle on: " + anyToggleOn); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### Accessing the Selected Toggle |
| 105 | + |
| 106 | +```csharp |
| 107 | +public ExtensionsToggleGroup toggleGroup; |
| 108 | + |
| 109 | +void Update() |
| 110 | +{ |
| 111 | + if (Input.GetKeyDown(KeyCode.E)) |
| 112 | + { |
| 113 | + var selected = toggleGroup.SelectedToggle; |
| 114 | + if (selected != null) |
| 115 | + { |
| 116 | + Debug.Log("Currently selected: " + selected.UniqueID); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### Allowing Multiple Off State |
| 123 | + |
| 124 | +```csharp |
| 125 | +public ExtensionsToggleGroup toggleGroup; |
| 126 | + |
| 127 | +void Start() |
| 128 | +{ |
| 129 | + // Allow all toggles to be off |
| 130 | + toggleGroup.AllowSwitchOff = true; |
| 131 | + |
| 132 | + // Now users can click the selected toggle again to deselect it |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### Querying Active Toggles |
| 137 | + |
| 138 | +```csharp |
| 139 | +public ExtensionsToggleGroup toggleGroup; |
| 140 | + |
| 141 | +void CheckActiveToggles() |
| 142 | +{ |
| 143 | + var activeToggles = toggleGroup.ActiveToggles(); |
| 144 | + foreach (var toggle in activeToggles) |
| 145 | + { |
| 146 | + Debug.Log("Active toggle: " + toggle.UniqueID); |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### Controlling Toggles Programmatically |
| 152 | + |
| 153 | +```csharp |
| 154 | +public ExtensionsToggleGroup toggleGroup; |
| 155 | +public ExtensionsToggle[] optionToggles; |
| 156 | + |
| 157 | +void SelectOption(int index) |
| 158 | +{ |
| 159 | + if (index >= 0 && index < optionToggles.Length) |
| 160 | + { |
| 161 | + optionToggles[index].IsOn = true; |
| 162 | + // Other toggles in the group will automatically turn off |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +void DeselectAll() |
| 167 | +{ |
| 168 | + if (toggleGroup.AllowSwitchOff) |
| 169 | + { |
| 170 | + toggleGroup.SetAllTogglesOff(); |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +--------- |
| 176 | + |
| 177 | +## Video Demo |
| 178 | + |
| 179 | +Coming soon... |
| 180 | + |
| 181 | +--------- |
| 182 | + |
| 183 | +## See also |
| 184 | + |
| 185 | +* [ExtensionsToggle](/Controls/ExtensionsToggle.md) |
| 186 | +* [UIButton](/Controls/UIButton.md) |
| 187 | +* [SegmentedControl](/Controls/SegmentedControl.md) |
| 188 | + |
| 189 | +--------- |
| 190 | + |
| 191 | +## Credits and Donation |
| 192 | + |
| 193 | +Unity UI Extensions Contributors |
| 194 | + |
| 195 | +--------- |
| 196 | + |
| 197 | +## External links |
| 198 | + |
| 199 | +[Unity ToggleGroup Documentation](https://docs.unity3d.com/Manual/script-ToggleGroup.html) |
0 commit comments