UIToggle

Code examples to perform various interactions with the UIToggle

Get the native Toggle component

This example shows how to get a reference to the native Toggle component that is attached to the same GameObject as the UIToggle component. You don’t really need this reference, but should you have a particular use-case where you need it this is the way to get it.

using Doozy.Engine.UI;
using UnityEngine;
using UnityEngine.UI;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        Toggle toggle = MyToggle.Toggle; //get a reference to the native Toggle component attached to the same GameObject as the UIToggle component
    }
}

Toggle On

Toggles the target UIToggle as On (checked)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
       MyToggle.ToggleOn();
       
       //OR

       MyToggle.IsOn = true;
    }
}

Toggle Off

Toggles the target UIToggle as On (checked)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
       MyToggle.ToggleOff();
       
       //OR

       MyToggle.IsOn = false;
    }
}

Is On

Check if the target UIToggle is On or Off (checked or unchecked)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        bool isOn = MyToggle.IsOn;

        Debug.Log("The UIToggle is " + (isOn ? "ON" : "OFF"));
    }
}

Select toggle (to the EventSystem)

Set the target UIToggle as the selected object in the EventSystem

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.SelectToggle();
    }
}

Deselect toggle (from the EventSystem)

Deselect the target UIToggle from the EventSystem (if selected)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.DeselectToggle();
    }
}

Deselect toggle (from the EventSystem) – after a set delay

Deselect the target UIToggle from the EventSystem (if selected), after a set delay.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;
    public float DeselectDelay = 3f; //3 seconds delay

    private void Start()
    {
        MyToggle.DeselectToggle(DeselectDelay);
    }
}

Disable toggle

Disable the target UIToggle. This sets the Interactable property to false.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.DisableToggle();
    }
}

Disable toggle – for a set duration

Disable the target UIToggle for a set time duration. This sets the Interactable property to false (for a set duration).

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;
    public float DisableDuration = 2f; //2 seconds

    private void Start()
    {
        MyToggle.DisableToggle(DisableDuration);
    }
}

Enable toggle

Enable the target UIToggle. This sets the Interactable property to true.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.EnableToggle();
    }
}

Execute OnPointerEnter

Execute OnPointerEnter for the target UIToggle. Note that OnPointerEnter needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.ExecutePointerEnter();
    }
}

Execute OnPointerExit

Execute OnPointerExit for the target UIToggle. Note that OnPointerExit needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.ExecutePointerExit();
    }
}

Execute OnClick

Execute OnClick for the target UIToggle. Note that OnClick needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.ExecuteClick();
    }
}

Execute OnSelected

Execute OnSelected for the target UIToggle. Note that OnSelected needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.ExecuteOnButtonSelected();
    }
}

Execute OnDeselected

Execute OnDeselected for the target UIToggle. Note that Ondeslected needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        MyToggle.ExecuteOnButtonDeselected();
    }
}

Has Label

Check if the target UIToggle has a label referenced. A label can be either a Text or a TextMeshPro component.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        bool hasLabel = MyToggle.HasLabel;
        
        Debug.Log("The target UIToggle " + (hasLabel ? "has" : "does not have") + " a label referenced.");
    }
}

Set Label Text

If the target UIToggle has a label referenced (this can be either a Text or TextMeshPro component), the text value will get updated with the passed string.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;
    public string LabelText = "My Toggle Label";

    private void Start()
    {
       MyToggle.SetLabelText(LabelText);
    }
}

Is Interactable

Check if the target UIToggle is interactable. This value is linked to the native Toggle component that is attached to the same GameObject as the target UIToggle.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        bool isInteractable = MyToggle.Interactable;

        Debug.Log("The UIToggle is " + (isInteractable ? "interactable" : "not interactable"));
    }
}

Is Selected

Check if the target UIToggle is selected by the EventSystem. This actually checks if the EventSystem.current.currentSelectedGameObject is the target UIToggle.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIToggle MyToggle;

    private void Start()
    {
        bool isSelected  = MyToggle.IsSelected;

        Debug.Log("The UIToggle is " + (isSelected  ? "selected" : "not selected"));
    }
}