1. What is a ToggleGroup component?
ToggleGroup is a component in the Unity UGUI that manages the selection status of a set of Toggle (options). The ToggleGroup component can ensure that only one Toggle is selected in the same ToggleGroup, and other Toggles will automatically uncheck it.
2. How ToggleGroup Components Work
The ToggleGroup component implements management functions by listening to Toggle's selection status. When a Toggle is selected, the ToggleGroup will iterate over other Toggles in the same group and cancel their selected status.
3. Common properties of ToggleGroup components
- AllowSwitchOff (Allows deselect): Set whether to allow deselecting. If set to true, all Toggles can be unchecked; if set to false, at least one Toggles will be always selected.
4. Common functions of ToggleGroup components
- NotifyToggleOn(Toggle toggle): Notify ToggleGroup that a Toggle is selected. The function is automatically called in Toggle's OnValueChanged event without manual call.
5. Complete example
Example 1: Create ToggleGroup and Toggle
using UnityEngine; using ; public class Example1 : MonoBehaviour { public ToggleGroup toggleGroup; public Toggle toggle1; public Toggle toggle2; private void Start() { = toggleGroup; = toggleGroup; } }
Operation steps:
- Create an empty object and mount the Example1 script to the object.
- Create two Toggles in the scene and drag their Toggle components into references to ggle1 and toggle2, respectively.
- Drag the toggleGroup component into the toggleGroup reference.
- Run the game, click toggle1 or toggle2, and observe their selected status.
Things to note:
- The group properties of toggle1 and toggle2 must be set to toggleGroup.
Example 2: Dynamically create Toggle
using UnityEngine; using ; public class Example2 : MonoBehaviour { public ToggleGroup toggleGroup; public GameObject togglePrefab; public Transform toggleParent; private void Start() { for (int i = 0; i < 5; i++) { GameObject toggleObj = Instantiate(togglePrefab, toggleParent); Toggle toggle = <Toggle>(); = toggleGroup; } } }
Operation steps:
- Create an empty object and mount the Example2 script to the object.
- Create a Toggle prefab togglePrefab and drag it into the togglePrefab reference.
- Create an empty object toggleParent and drag it into the reference of toggleParent.
- Drag the toggleGroup component into the toggleGroup reference.
- Run the game and observe the number of Toggle and selected status under toggleParent.
Things to note:
- togglePrefab must include the Toggle component.
- toggleParent must be a container for storing dynamically created Toggle.
Example 3: Unselect
using UnityEngine; using ; public class Example3 : MonoBehaviour { public ToggleGroup toggleGroup; public Button cancelButton; private void Start() { (CancelSelection); } private void CancelSelection() { (); } }
Operation steps:
- Create an empty object and mount the Example3 script to the object.
- Create a Button and drag it into the cancelButton reference.
- Drag the toggleGroup component into the toggleGroup reference.
- Run the game, click cancelButton, and observe whether Toggle in toggleGroup has been deselected.
Things to note:
- cancelButton must be a Button, and the OnClick event has been added.
Example 4: Get the selected Toggle
using UnityEngine; using ; public class Example4 : MonoBehaviour { public ToggleGroup toggleGroup; public Button getSelectedButton; private void Start() { (GetSelectedToggle); } private void GetSelectedToggle() { Toggle selectedToggle = ().FirstOrDefault(); if (selectedToggle != null) { ("Selected Toggle: " + ); } else { ("No Toggle selected."); } } }
Operation steps:
- Create an empty object and mount the Example4 script to the object.
- Create a Button and drag it into the reference of getSelectedButton.
- Drag the toggleGroup component into the toggleGroup reference.
- Run the game, click getSelectedButton, and observe the console output.
Things to note:
- getSelectedButton must be a Button, and the OnClick event has been added.
Example 5: Disable ToggleGroup
using UnityEngine; using ; public class Example5 : MonoBehaviour { public ToggleGroup toggleGroup; public Button disableButton; private void Start() { (DisableToggleGroup); } private void DisableToggleGroup() { = false; } }
Operation steps:
- Create an empty object and mount the Example5 script to the object.
- Create a Button and drag it into the reference of the disableButton.
- Drag the toggleGroup component into the toggleGroup reference.
- Run the game, click disableButton, and observe whether toggleGroup is disabled.
Things to note:
- disableButton must be a Button, and the OnClick event has been added.
References
- Unity official documentation:ToggleGroup
- Unity official tutorial:Toggle
The above is the detailed introduction to the ToggleGroup option component of Unity UGUI. For more information about the Unity UGUI ToggleGroup option component, please follow my other related articles!