- Home /
How can I get UI element over which pointer is in Unity3D?
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.
Unity 5.3.4.
Answer by Roland1234 · Jul 28, 2016 at 07:04 AM
Simply newing up a PointerEventData
object will not initialize it with the actual data you're looking for. It sounds to me like you're wanting to get the PointerEventData
from your current input module. Problem is that the relevant input module members are not publicly exposed - but they do have protected access, so you can derive your own input module and expose them that way. For example, if you're using the default StandaloneInputModule
:
using UnityEngine.EventSystems;
public class ExtendedStandaloneInputModule : StandaloneInputModule
{
public static PointerEventData GetPointerEventData(int pointerId = -1)
{
PointerEventData eventData;
_instance.GetPointerData(pointerId, out eventData, true);
return eventData;
}
private static ExtendedStandaloneInputModule _instance;
protected override void Awake()
{
base.Awake();
_instance = this;
}
}
Use something like the above to replace the input module in your EventSystem object (may have to set "Force Module Active" for it to work), and you can call the static ExtendedStandaloneInputModule.GetPointerEventData()
method to get proper PointerEventData
objects from it. In your case you will likely be wanting to look at the hovered
member which is a List<GameObject>
of objects in the hover stack.
The EventSystem keeps a dictionary of PointerEventData
objects keyed by pointer ids, where the pointer ids refer to specific pointer events: -1, -2, and -3 are left, right, and middle mouse buttons; and I believe 1, 2, 3+ refer to first, second, third, touch input fingers, etc. but I'm not too familiar with touch controls. I'm assuming any mouse button pointer id will yield the hovered
members you'll be interested in, if not then at least the default -1 pointer id should work.
Hope that helps!
Answer by alex_ohadi · Apr 10, 2019 at 09:35 PM
Try this in your foreach loop
if(go.gameobject == this.gameobject) { // go.gameobject is not the UI element you are hovering over }
Your answer
Follow this Question
Related Questions
IPointerEnterHander only fires on click 0 Answers
GameObject rapidly changing position (flickering) 1 Answer
Put mouse to sleep 1 Answer
Get object at mouse position? (UI) 0 Answers
Re-center UI Image after zooming out 0 Answers