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?