2
\$\begingroup\$

I can detect if mouse is hovering any UI element by EventSystem.current.IsPointerOverGameObject().

But how do I know which GameObjest it is exactly?

I have tried:

    if (EventSystem.current.IsPointerOverGameObject())
        foreach (GameObject go in new PointerEventData(EventSystem.current).hovered)
            print(go.name);

But in every moment new PointerEventData(EventSystem.current).hovered is empty whether I hover or not.

I can see required info using print(EventSystem.current);:

<b>Selected:</b>


<b>Pointer Input Module of type: </b>UnityEngine.EventSystems.StandaloneInputModule
<B>Pointer:</b> -1
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>: 
<b>lastPointerPress</b>: 
<b>pointerDrag</b>: 
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera: 
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>


<B>Pointer:</b> -2
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>: 
<b>lastPointerPress</b>: 
<b>pointerDrag</b>: 
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera: 
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>


<B>Pointer:</b> -3
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>: 
<b>lastPointerPress</b>: 
<b>pointerDrag</b>: 
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera: 
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>

By the way why are there 3 pointers (-1, -2, -3) and what does this mean? I've read somewhere in docs, pointer -1 is left mouse button, but I click nothing, so this is weird.

I can see <b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject) and Name: cursorInfoText (UnityEngine.GameObject) which is what I need. But how do I extract this info? I have tried print(new PointerEventData(EventSystem.current).pointerEnter);, but it is Null doesn't matter I enter or hover. And I see no other appropriate methods or properties in docs, although I can see that info is stored. I've tried EventSystem.current.currentSelectedGameObject too and it always returns Null as well. What have I missed?

My goal is to detect if mouse is hovering UI element with some conditions (ignore small amount of UI elements (likely by tag) and don't ignore the rest (most of elements)), so I have to get GameObject itself. And if mouse doesn't hover UI element (except those few), script does unrelated to UI stuff. But do not ignore if it hovers one of those few and one of the rest at the same time.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Unity 4.6 UI doesn't really like those OnMouse events, so we can't use OnMouseOver() for example. (Those are just for Colliders or GUIElements.) Instead, we use Pointer properties from Selectable under EventSystem. You can add the following script on your Canvas, or individually on UI transforms, or just attach EventListeners to them.

using UnityEngine;
using UnityEngine.EventSystems; //required for Event data

public class MouseOver : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {

    GameObject currentHover;

    public void OnPointerEnter(PointerEventData eventData) {
        if (eventData.pointerCurrentRaycast.gameObject != null) {
            Debug.Log("Mouse Over: " + eventData.pointerCurrentRaycast.gameObject.name);
            currentHover = eventData.pointerCurrentRaycast.gameObject;
        }
    }

    public void OnPointerExit(PointerEventData eventData) {
        currentHover = null;
    }

    void Update() {
        if (currentHover)
            Debug.Log(currentHover.name + " @ " + Input.mousePosition);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ This helped me get going in the right direction, the eventData is a bit vast lol \$\endgroup\$
    – kabuto178
    Commented May 22, 2017 at 15:16
1
\$\begingroup\$

Sorry, I'm late. I answer in case you haven't found a solution yet.

There are several solutions to detect the GameObject the pointer is over, I think that the best is the use of EventTrigger. Usage is quite simple, your script will be something similar:

using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
     OnMouseOver(string str) {
          Debug.Log(str);
    }

}

Now select the GameObject you want detect, Inspector, Add Component and search for EventTrigger, then Add New Event Type and Pointer Enter. Now drag and drop the GameObject that you've applied the script in the apposite field, in the menu search function OnMouseOver and type a string that allows you to recognize the object on which the mouse is on. You can repeat the process with Pointer Exit, obviously using a different string. Now whenever your mouse is over the GameObject, the function OnMouseOver will be executed.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .