UIButton

Code examples to perform various interactions with the UIButton

Get the native Button component

This example shows how to get a reference to the native Button component that is attached to the same GameObject as the UIButton 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 UIButton MyButton;

    private void Start()
    {
        Button button = MyButton.Button; //get a reference to the native Button component attached to the same GameObject as the UIButton component
    }
}

Select button (to the EventSystem)

Set the target UIButton as the selected object in the EventSystem

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.SelectButton();
    }
}

Deselect button (from the EventSystem)

Deselect the target UIButton from the EventSystem (if selected)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.DeselectButton();
    }
}

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

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;
    public float DeselectDelay = 3f; //3 seconds delay

    private void Start()
    {
        MyButton.DeselectButton(DeselectDelay);
    }
}

Disable button

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.DisableButton();
    }
}

Disable button – for a set duration

Disable the target UIButton 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 UIButton MyButton;
    public float DisableDuration = 2f; //2 seconds

    private void Start()
    {
        MyButton.DisableButton(DisableDuration);
    }
}

Enable button

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.EnableButton();
    }
}

Execute OnPointerEnter

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

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

Execute OnPointerExit

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

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

Execute OnPointerDown

Execute OnPointerDown for the target UIButton. Note that OnPointerDown needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.ExecutePointerDown();
    }
}

Execute OnPointerUp

Execute OnPointerUp for the target UIButton. Note that OnPointerUp needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.ExecutePointerUp();
    }
}

Execute OnClick

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

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

Execute OnDoubleClick

Execute OnDoubleClick for the target UIButton. Note that OnDoubleClick needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.ExecuteDoubleClick();
    }
}

Execute OnLongClick

Execute OnLongClick for the target UIButton. Note that OnLongClick needs to be enabled for this to work.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.ExecuteLongClick();
    }
}

Execute OnSelected

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

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

Execute OnDeselected

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

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

Start Normal Loop Animation

Starts playing the Normal Loop Animation for the target UIButton. Note that the Normal Loop Animation needs to be enabled for this to work.
The Normal Loop Animation is used when the button is not selected (in idle mode)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.StartNormalLoopAnimation();
    }
}

Stop Normal Loop Animation

Stops playing the Normal Loop Animation for the target UIButton.
The Normal Loop Animation is used when the button is not selected (in idle mode)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.StopNormalLoopAnimation();
    }
}

Start Selected Loop Animation

Starts playing the Selected Loop Animation for the target UIButton. Note that the Selected Loop Animation needs to be enabled for this to work.
The Selected Loop Animation is used when the button is selected (by the EventSystem)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.StartSelectedLoopAnimation();
    }
}

Stop Selected Loop Animation

Stops playing the Selected Loop Animation for the target UIButton.
The Selected Loop Animation is used when the button is selected (by the EventSystem)

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        MyButton.StopSelectedLoopAnimation();
    }
}

Has Label

Check if the target UIButton 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 UIButton MyButton;

    private void Start()
    {
        bool hasLabel = MyButton.HasLabel;

        Debug.Log("The '" + MyButton.ButtonName + "' UIButton " + (hasLabel ? "has" : "does not have") + " a label referenced.");
    }
}

Set Label Text

If the target UIButton 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 UIButton MyButton;
    public string LabelText = "My Button Label";

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

Is Interactable

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

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

        Debug.Log("The '" + MyButton.ButtonName + "' UIButton " + (isInteractable ? "is" : "is not") + " interactable.");
    }
}

Is Selected

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

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

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

        Debug.Log("The '" + MyButton.ButtonName + "' UIButton " + (isSelected ? "is" : "is not") + " selected by the EventSystem.");
    }
}

Is Back Button

Check if the target UIButton is a ‘Back’ button. A UIButton is a ‘Back’ button if its button name is ‘Back’.

using Doozy.Engine.UI;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public UIButton MyButton;

    private void Start()
    {
        bool isBackButton = MyButton.IsBackButton;

        Debug.Log("The '" + MyButton.ButtonName + "' UIButton " + (isBackButton ? "is" : "is not") + " a 'Back' button.");
    }
}