Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

EventSystem - Get current GO under pointer?

Discussion in 'Unity UI & TextMesh Pro' started by Democide, Jan 10, 2019.

  1. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    274
    Hey y'all,

    I'm looking for a quick way (cause it's in Update) to get the object currently under the pointer. Looking at the PointerInputModule and the PointerEventData there's pointerEventData.pointerEnter which is the GO but I don't have access to that in update, and EventSystem only gives me access to IsPointerOverGameObject().

    I'm asking because I want to check if the object is actually interactable and then create different behaviors based on that.
     
  2. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    16
    You can inherit the standalone input module and retrieve the cached data from the event system.

    Code (CSharp):
    1. public class CustomStandaloneInputModule : StandaloneInputModule
    2. {
    3.     public GameObject GetHovered()
    4.     {
    5.         var mouseEvent = GetLastPointerEventData(-1);
    6.         if (mouseEvent == null)
    7.             return null;
    8.         return mouseEvent.pointerCurrentRaycast.gameObject;
    9.     }
    10.  
    11.     public List<GameObject> GetAllHovered()
    12.     {
    13.         var mouseEvent = GetLastPointerEventData(-1);
    14.         if (mouseEvent == null)
    15.             return null;
    16.         return mouseEvent.hovered;
    17.     }
    18. }
     
    Democide likes this.
  3. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    274
    Mhm. I was afraid something like this was needed. It's a bit weird that this is not readily available tho
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    1,001
    @Democide

    Or you could raycast using GraphicRaycaster, only when you are over some object?

    Code (CSharp):
    1.    
    2. List<RaycastResult> RaycastUI()
    3. {
    4.     List<RaycastResult> results = new List<RaycastResult>();
    5.     gr.Raycast(new PointerEventData(EventSystem.current) { position = Input.mousePosition }, results);
    6.     return results;
    7. }
    8.  
     
    Democide likes this.