EventSystem.current.IsPointerOverGameObject() for UIToolkit ?

In the old UI system we have EventSystem.current.IsPointerOverGameObject() to check if mouse is on a UI element. Is there anything similar for UIToolkit?

My goal: When I click on UI I need to ignore the click in other parts of the game.

visualElement.ContainsPoint(Input.mousePosition)

seems to work, but is not perfect.

1 Like

To clarify, it works in most cases. Just run ContainsPoint on the root VisualElement. But in my case I have a panel on the left and one on the right with space between (using Align > Justify Content: space-between). Running ContainsPoint on the root returns true in the space between as well as in the panels.

I’ll just run ContainsPoint on each panel instead. No big deal. But with a more complex interface this could quickly become cumbersome.

1 Like

Oof.

Thanks for sharing.

1 Like

You’re welcome.

Update: Doing ContainsPoint on each panel doesn’t work. The right one simply always returns false. Left works fine.

ContainsPoints() expects the coordinates to be transformed to its local reference. You can do:

var localPoint = rightPanel.WorldToLocal(point);
if(rightPanel.ContainsPoint(localPoint)){...}
3 Likes

Thanks, that works.

1 Like