- Home /
How do I trigger OnSelect() via the ISelectHandler in my class?
I'm trying to figure out how I can make a custom UI element identify when it has been selected or deselected. Classes such as Selectable
inherit from the ISelectHandler
and IDeselectHandler
and their methods OnSelect()
and OnDeselect()
are called whenever I click inside the Selectable
element and click outside of it.
From my understanding, inheriting from the appropriate handler interface and implementing it's required method will allow your class to handle events from the EventSystem
. As an example if I have:
public class TestSelectable
: MaskableGraphic,
ISelectHandler,
IDeselectHandler,
IPointerEnterHandler,
IPointerExitHandler
{
public void OnSelect(BaseEventData eventData)
{
Debug.Log("Selected");
}
public void OnDeselect(BaseEventData eventData)
{
Debug.Log("De-Selected");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Pointer Enter");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Pointer Exit");
}
When the mouse enters and exits the element the OnPointerEnter()
and OnPointerExit()
methods are sucessfully called. However selecting/De-selecting the element does not cause the OnSelect()
or OnDeselect()
methods to be called. Though if you look in InputField.cs
(which inherits from Selectable
), the OnSelect()
method does successfully get called when the input field is selected. I'm trying to replicate this within my own element without having to inherit from Selectable
, and since Unity's event system is publicly accessible this should be possible.
Are there other requirements that will enable those events to fire and call my methods?
Edit: Browsed through the selectable
source on bitbucket and was unable to gleam anything from that.
Answer by seantheyahn · Jun 07, 2015 at 03:53 PM
In my case this worked:
public void OnPointerDown(PointerEventData eventData)
{
eventData.selectedObject = gameObject;
}
(after this OnSelect and OnDeselect worked)
Your answer
Follow this Question
Related Questions
ScrollRect and IDragHandler on touch devices 1 Answer
Trying to make a button interactable 3 Answers
How do I stop EventSystem.currentSelectedGameObject from changing on a button press 0 Answers
inheritance vs multiple component for UI 1 Answer
How can i add a event onclick for a ui button that is child of canvas ? 1 Answer