Class Toggle
A standard toggle that has an on / off state.
Inherited Members
UnityEngine.Component.GetComponentInParent<T>(System.Boolean)
UnityEngine.Object.FindObjectsByType<T>(UnityEngine.FindObjectsSortMode)
UnityEngine.Object.FindObjectsOfType<T>(System.Boolean)
UnityEngine.Object.FindObjectsByType<T>(UnityEngine.FindObjectsInactive, UnityEngine.FindObjectsSortMode)
UnityEngine.Object.FindObjectOfType<T>(System.Boolean)
UnityEngine.Object.FindFirstObjectByType<T>()
UnityEngine.Object.FindAnyObjectByType<T>()
UnityEngine.Object.FindFirstObjectByType<T>(UnityEngine.FindObjectsInactive)
UnityEngine.Object.FindAnyObjectByType<T>(UnityEngine.FindObjectsInactive)
Namespace: UnityEngine.UI
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 |
---|---|
Boolean |
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()
Declaration
public virtual void GraphicUpdateComplete()
Implements
LayoutComplete()
Declaration
public virtual void LayoutComplete()
Implements
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 |
Implements
OnSubmit(BaseEventData)
Declaration
public virtual void OnSubmit(BaseEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
BaseEventData | eventData |
Implements
Rebuild(CanvasUpdate)
Declaration
public virtual void Rebuild(CanvasUpdate executing)
Parameters
Type | Name | Description |
---|---|---|
CanvasUpdate | executing |
Implements
SetIsOnWithoutNotify(Boolean)
Set isOn without invoking onValueChanged callback.
Declaration
public void SetIsOnWithoutNotify(bool value)
Parameters
Type | Name | Description |
---|---|---|
Boolean | value | New Value for isOn. |
Start()
Assume the correct visual state.
Declaration
protected override void Start()