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.