Search Unity

How to get button that's under cursor?

Discussion in 'UGUI & TextMesh Pro' started by User340, Aug 4, 2019.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    How do I get the button that is currently under the cursor?
     
  2. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    39
    Hey Daniel,
    The best way I've found to do this is inherit from the StandaloneInputModule. Something like this for example:

    public class CustomStandaloneInputModule : StandaloneInputModule
    {
    public GameObject GetHovered()
    {
    var mouseEvent = GetLastPointerEventData(-1);
    if (mouseEvent == null)
    return null;
    return mouseEvent.pointerCurrentRaycast.gameObject;
    }

    public List<GameObject> GetAllHovered()
    {
    var mouseEvent = GetLastPointerEventData(-1);
    if (mouseEvent == null)
    return null;
    return mouseEvent.hovered;
    }
    }