- Home /
If button highlighted
Hi there,
My question is a short one. I've been looking for a reference on a Canvas selectable button. The button itself does not call a function when clicked, it should just wait to see if it has been highlighted.
I have a script that was put directly on the button itself but I do not know how to write an "if statement" on "if this button is selected then.." in C#.
Answer by DoTA_KAMIKADzE · Apr 20, 2015 at 12:07 AM
Add script containing this code to your Button:
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class YourScript : MonoBehaviour, IPointerEnterHandler, ISelectHandler
{
public void OnPointerEnter(PointerEventData eventData)
{
//do your stuff when highlighted
}
public void OnSelect(BaseEventData eventData)
{
//do your stuff when selected
}
}
Thank you very much it seemed to work using the mouse.. Though I do have one issue.
When I select the button using the navigation on the keyboard, it does not read as being highlighted because a pointer is not being used.
How would I go about reading if it is highlighted even without pointer?
Use OnPointerExit or OnDeselect for that. The EventTrigger article lists the various events you can intercept.
Answer by sisse008 · Apr 29, 2018 at 01:04 PM
cant find any new info on this topic. OnPointerEnter() only works with the mouse.
How can I check if a button is highlighted while using keyboard navigation?
thank you