Checking if over UI during onEndDrag - EventSystem.isPointerOverGameObject is not working as expected
I've built a basic drag and drop, from icons in the UI into the scene.
I'm currently having an issue with detecting if while onEndDrag the user is still over the UI as I want to block item creation.
I'm using the standard method to test if the user is still over the UI, but it is always returning true. I imagine this as the start of the drag and drop is from the UI. - Is there anyway to get around this? - Is this a potential bug?
bool IsPointerOverGameObject(int fingerId)
{
EventSystem eventSystem = EventSystem.current;
return (eventSystem.IsPointerOverGameObject(fingerId)
&& eventSystem.currentSelectedGameObject != null);
}
Above is my C# implementation to check if the pointer is over the UI. This is called in the onEndDrag function.
public virtual void OnEndDrag(PointerEventData eventData)
{
..
if (!IsPointerOverGameObject(eventData.pointerId))
..
}
Any help would be highly recommended.
Anyone had any luck with checking if pointer is over UI within OnEndDrag?
Just to be sure. You are not moving the object you're dragging and you release the pointer outside the object?
Pretty sure, I will give it another test.
- Will make sure to disable as many canvas UI elements as I can as well.
On a side note: I do a number of other isPointerOverUI checks which all work as expected, notably this is the only one within an OnEndDrag handler.
So I've ran through my code and implementation, I've added this log line to display which game object it thinks the pointer is over.
Debug.Log("StandardDragDropCursor - OnEndDrag - OVER GA$$anonymous$$E OBJECT: " + EventSystem.current.currentSelectedGameObject.name);
Example output:
StandardDragDropCursor - OnEndDrag - OVER GA$$anonymous$$E OBJECT: Bedroom2_Icon UnityEngine.Debug:Log(Object) StandardDragDropCursor:OnEndDrag(PointerEventData) (at Assets/......../StandardDragDropCursor.cs:95) UnityEngine.EventSystems.EventSystem:Update()
This log will always output the name of the UI element I activate the Drag Handlers, regardless if I drag my pointer over another UI element or no UI element.
How strange, I'm 90% sure this is a bug. Or it is intended, if Unity has some sort of assumption that on drag brings the UI element with it?
I'm going to give this a go: Rather than use eventData from the handle, get the input position using mousePointer potentially.
Answer by aaronp261 · Mar 04, 2017 at 02:34 PM
So I've found a solution,
Implementing both IPointerExitHandler, IPointerEnterHandler and storing IsOverUI in a private bool so I can use this to check, it's not a perfect solution but the only usable one I've found.
If anyone has experienced this or solved with a more complete solution that would be great.