Control pad menu navigation - mouse cursor still highlighting / selecting button when disabled

I’ve sorted it. And by “sorted it” I mean I have no idea if what I have done is a good thing, and it’ll probably blow up.

I found this script here from @daterre : [Button] Keyboard and Mouse Highlighting

This solved the issue, but it caused a new problem for me where buttons wouldn’t deselect when the mouse pointer stopped being over it.

My fix was to further modify the script to this:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


[RequireComponent(typeof(Selectable))]
public class HighlightFix : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IDeselectHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (!EventSystem.current.alreadySelecting)
        {
            EventSystem.current.SetSelectedGameObject(this.gameObject);
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        EventSystem.current.SetSelectedGameObject(null);
    }

    public void OnDeselect(BaseEventData eventData)
    {
        this.GetComponent<Selectable>().OnPointerExit(null);
    }
}

This seems to now work really nicely for me. When switching to control pad, any button that the mouse pointer was on is deselected.

1 Like