Astra Child

How to connect to a UIButton ui selection state event

Estimated reading: 1 minute 0 views

About UISelectionStates

The UISelectionState defines the states a UISelectable can be in. Their events are always enabled and available to connect to.

The UISelectionStates are:

  • Normal
  • Highlighted
  • Pressed
  • Selected
  • Disabled

Instructions

To connect to a ui selection state event from code you need to following:

  • Have a reference to the target UIButton
  • The example below shows how to connect to the Pressed ui selection state of a UIButton
using Doozy.Runtime.UIManager;
using Doozy.Runtime.UIManager.Components;
using UnityEngine;

namespace Sandbox
{
    public class TestClass : MonoBehaviour
    {
        public UIButton MyButton;

        private void OnEnable()
        {
            //Add event listener
            MyButton.GetUISelectableState(UISelectionState.Pressed).stateEvent.Event.AddListener(OnPressed);
        }

        private void OnDisable()
        {
            //Remove event listener
            MyButton.GetUISelectableState(UISelectionState.Pressed).stateEvent.Event.RemoveListener(OnPressed);
        }

        private void OnPressed()
        {
            //do stuff
        }
    }
}Code language: PHP (php)
Share this Doc
CONTENTS
Scroll to Top