1. What is the Toggle component?
Toggle (check box) is a common component in Unity UGUI that implements the function of check boxes. It can be selected or unchecked, and the code can be controlled through its state.
How Toggle Components Work
The Toggle piece consists of two parts: background marking and. The background is used to display the appearance of the check box indicative of the check box. When clicked by the component group Toggle, it will automatically switch the selected state and trigger the corresponding event.
Common properties of Toggle components
- Is OnIndicates: The selected status code of the Toggle component can be controlled through.
- Transition: Indicates the transition effect of group Toggle parts, including None, Color Tint, Sprite Swap and other options.
- Graphic: Represents the background image of the Toggle component.
- GroupToggle means: The group group belongs to Toggle.
On Value Changed: Indicates the event triggered when the Toggle component selects the status change.
Common functions of Toggle components
- void Toggle(): Toggle the selected status of the group Toggle.
- void OnClickPointer(PointerEventData eventData): Handle click events of Toggle component.
Sample code
Example 1: Create a simple Toggle component
- Create an object Canvas and create a Toggle object under Canvas.
- Set the Is On property of component Toggle to true.
- Run the game, click the Toggle component to observe whether the selected state of the Toggle component has changed.
Example 2: Use code to control the selected state of the Toggle component
- Create a Canvas object under and create a Toggle object under Canvas.
- Get a reference to the Toggle component in the script.
- Use the Is On property of the Toggle component in the code to control the selected state of the Toggle component.
using UnityEngine; using ; public class ToggleExample : MonoBehaviour { public Toggle toggle; private void Start() { toggle =.isOn true; } }
Example 3: Events using Toggle Components
- Create a Canvas object and create a Toggle object under Canvas.
- Add a method to the script to handle the selected state change event of the Toggle component.
- Bind this method to the On Value Change event of the Toggle component.
using UnityEngine; using ; public class ToggleExample : MonoBehaviour { public Toggle toggle; private void Start() { (OnToggleValueChanged); } private void OnToggleValueChanged(bool isOn) { ("Toggle is " + (isOn ? "on" : "off")); } }
Example 4: Transition effect using Toggle components
- Create a Canvas object and create a Toggle object under Canvas.
- Set the Transition property of the Toggle component to Colorint T.
- Set the Graphic property of the Toggle component to a background image.
- Run the game and observe the transition effects of the Toggle component.
Example 5: Toggle Group using Toggle Components
- Create a Canvas object and create two Toggle objects under Canvas.
- Create a ToggleGroup object and set the Group properties of the two Toggle objects to set the ToggleGroup object.
- Run the game and observe whether the selected state of the Toggle component is mutually exclusive.
Things to note
- The Toggle component must be placed under the Canvas object to display normally.
- The selected status of the Toggle component can also be controlled by code, and can be switched by clicking.
- The corresponding event will be triggered when the selected state of the Toggle component is changed.
References
Unity official documentation:Toggle
The above is the detailed explanation of the usage of the Toggle checkbox component of Unity UGUI. For more information about the Unity UGUI Toggle checkbox, please follow my other related articles!