Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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;
    }
    }