Astra Child

How to connect to a UIButton ui behaviour event

Estimated reading: 1 minute 0 views

UI Behaviour

The behaviours do not work by default to increase overall performance of the system. Thus if you want to use a specific UIBehaviour you need to add it first in the Inspector.

  • PointerEnter
  • PointerExit
  • PointerDown
  • PointerUp
  • PointerClick
  • PointerDoubleClick
  • PointerLongClick
  • PointerLeftClick
  • PointerMiddleClick
  • PointerRightClick
  • Selected
  • Deselected
  • Submit

Instructions

To connect to a behaviour event from code you need to following:

  • Add the desired behaviour (Pointer Click for example) to the target UIButton
  • Have a reference to the target UIButton
  • The example below shows how to connect to the Pointer Click ui behaviour on 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.AddBehaviour(UIBehaviour.Name.PointerClick).Event.AddListener(OnPointerClick);
        }

        private void OnDisable()
        {
            //Remove event listener
            MyButton.AddBehaviour(UIBehaviour.Name.PointerClick).Event.RemoveListener(OnPointerClick);
        }

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