- Home /
how to Select Button with mouse over?
it seems that when you navigate the UI with gamepad or keyboard the highlighted button is also selected, but when you use the mouse, the button is only highlighted but no selected.
Is there any way to select with mouse over instead just highlighting the button?
You could try implementing it. You'd need some ButtonExtension
class which would implement IPointerEnterHandler
and IPointerExitHandler
interfaces. Then use EventSystem.current.SetSelectedGameObject()
function to select your Button on pointer enter, and deselect it on pointer exit
Answer by CFazilleau · May 08, 2020 at 12:52 PM
Better than adding an Event Trigger,
Make a script implementing the IPointerEnterHandler
interface and calling the Select
method of your selectable (in that case, your button).
public class AutoSelect : MonoBehaviour, IPointerEnterHandler
{
[SerializeField]
private Selectable selectable = null;
public void OnPointerEnter(PointerEventData eventData)
{
selectable.Select();
}
}
Answer by Mmmpies · Jun 14, 2016 at 08:19 PM
Yes, add an Event Trigger to the button.
Then add a new event and select OnPointerEnter.
Have a script with a public function that reacts to that button now having the mouse over it. e.g.
public void MouseOverMe()
{
Put the script onto a game object in the scene and drag that object onto the empty slot created by the new event type and select YourScriptName -> MouseOverMe from the drop down to the right.
Now whatever you have in that function gets called when the mouse enters the button.