I am using EventSystem.current.IsPointerOverGameObject in a script and Unity returns True even though I would swear there is no UI/EventSystem object beneath the pointer.
How can I find information about the object the EventSystem is detecting?
In your update()
:
if(Input.GetMouseButton(0))
{
PointerEventData pointer = new PointerEventData(EventSystem.current);
pointer.position = Input.mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointer, raycastResults);
if(raycastResults.Count > 0)
{
foreach(var go in raycastResults)
{
Debug.Log(go.gameObject.name,go.gameObject);
}
}
}
I've stumbled upon this thread and wasn't quite happy with the solution provided, so here's another implementation to share:
public class StandaloneInputModuleV2 : StandaloneInputModule
{
public GameObject GameObjectUnderPointer(int pointerId)
{
var lastPointer = GetLastPointerEventData(pointerId);
if (lastPointer != null)
return lastPointer.pointerCurrentRaycast.gameObject;
return null;
}
public GameObject GameObjectUnderPointer()
{
return GameObjectUnderPointer(PointerInputModule.kMouseLeftId);
}
}
Looking at the EventSystem's editor output showing names for the GameObjects, I decided that some functionality was already there and there should be means of using it. After diving into the opened source of the forementioned EventSystem
and IsPointerOverGameObject
(overrided in PointerInputModule
), I thought it'll be easier to extend the default StandaloneInputModule
.
Usage is simple, just replace the default one added on the scene with the EventSystem and reference in code like:
private static StandaloneInputModuleV2 currentInput;
private StandaloneInputModuleV2 CurrentInput
{
get
{
if (currentInput == null)
{
currentInput = EventSystem.current.currentInputModule as StandaloneInputModuleV2;
if (currentInput == null)
{
Debug.LogError("Missing StandaloneInputModuleV2.");
// some error handling
}
}
return currentInput;
}
}
...
var currentGO = CurrentInput.GameObjectUnderPointer();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With