Class Toggle
A standard toggle that has an on / off state.
Implements
Inherited Members
Namespace: UnityEngine.UI
Assembly: UnityEngine.UI.dll
Syntax
[AddComponentMenu("UI/Toggle", 30)]
[RequireComponent(typeof(RectTransform))]
public class Toggle : Selectable, IMoveHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler, IPointerClickHandler, ISubmitHandler, IEventSystemHandler, ICanvasElement
Remarks
The toggle component is a Selectable that controls a child graphic which displays the on / off state. When a toggle event occurs a callback is sent to any registered listeners of UI.Toggle._onValueChanged.
Constructors
Toggle()
Declaration
protected Toggle()
Fields
graphic
Graphic the toggle should be working with.
Declaration
public Graphic graphic
Field Value
Type | Description |
---|---|
Graphic |
onValueChanged
Allow for delegate-based subscriptions for faster events than 'eventReceiver', and allowing for multiple receivers.
Declaration
public Toggle.ToggleEvent onValueChanged
Field Value
Type | Description |
---|---|
Toggle.ToggleEvent |
Examples
//Attach this script to a Toggle GameObject. To do this, go to Create>UI>Toggle.
//Set your own Text in the Inspector window
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
Toggle m_Toggle;
public Text m_Text;
void Start()
{
//Fetch the Toggle GameObject
m_Toggle = GetComponent<Toggle>();
//Add listener for when the state of the Toggle changes, to take action
m_Toggle.onValueChanged.AddListener(delegate {
ToggleValueChanged(m_Toggle);
});
//Initialise the Text to say the first state of the Toggle
m_Text.text = "First Value : " + m_Toggle.isOn;
}
//Output the new state of the Toggle into Text
void ToggleValueChanged(Toggle change)
{
m_Text.text = "New Value : " + m_Toggle.isOn;
}
}
toggleTransition
Transition mode for the toggle.
Declaration
public Toggle.ToggleTransition toggleTransition
Field Value
Type | Description |
---|---|
Toggle.ToggleTransition |
Properties
group
Group the toggle belongs to.
Declaration
public ToggleGroup group { get; set; }
Property Value
Type | Description |
---|---|
ToggleGroup |
isOn
Whether the toggle is currently active.
Declaration
public bool isOn { get; set; }
Property Value
Type | Description |
---|---|
bool |
Examples
/Attach this script to a Toggle GameObject. To do this, go to Create>UI>Toggle.
//Set your own Text in the Inspector window
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
Toggle m_Toggle;
public Text m_Text;
void Start()
{
//Fetch the Toggle GameObject
m_Toggle = GetComponent<Toggle>();
//Add listener for when the state of the Toggle changes, and output the state
m_Toggle.onValueChanged.AddListener(delegate {
ToggleValueChanged(m_Toggle);
});
//Initialize the Text to say whether the Toggle is in a positive or negative state
m_Text.text = "Toggle is : " + m_Toggle.isOn;
}
//Output the new state of the Toggle into Text when the user uses the Toggle
void ToggleValueChanged(Toggle change)
{
m_Text.text = "Toggle is : " + m_Toggle.isOn;
}
}
Methods
GraphicUpdateComplete()
Callback sent when this ICanvasElement has completed Graphic rebuild.
Declaration
public virtual void GraphicUpdateComplete()
LayoutComplete()
Callback sent when this ICanvasElement has completed layout.
Declaration
public virtual void LayoutComplete()
OnDestroy()
Declaration
protected override void OnDestroy()
Overrides
OnDidApplyAnimationProperties()
Declaration
protected override void OnDidApplyAnimationProperties()
Overrides
OnDisable()
Declaration
protected override void OnDisable()
Overrides
OnEnable()
Declaration
protected override void OnEnable()
Overrides
OnPointerClick(PointerEventData)
React to clicks.
Declaration
public virtual void OnPointerClick(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
OnSubmit(BaseEventData)
Declaration
public virtual void OnSubmit(BaseEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
BaseEventData | eventData |
OnValidate()
Declaration
protected override void OnValidate()
Overrides
Rebuild(CanvasUpdate)
Rebuild the element for the given stage.
Declaration
public virtual void Rebuild(CanvasUpdate executing)
Parameters
Type | Name | Description |
---|---|---|
CanvasUpdate | executing | The current CanvasUpdate stage being rebuild. |
SetIsOnWithoutNotify(bool)
Set isOn without invoking onValueChanged callback.
Declaration
public void SetIsOnWithoutNotify(bool value)
Parameters
Type | Name | Description |
---|---|---|
bool | value | New Value for isOn. |
Start()
Assume the correct visual state.
Declaration
protected override void Start()