How to generically know if a click was handled (to prevent UI 'click through')

I’m having a hard time finding the best way to prevent UI Toolkit clicks from affecting gameplay.

Is there a way to handle an event that is raised if no toolkit element has handled the event? Or a way in a centralized update function to check if the frames click event was handled in the UI? Or is there something else I should be doing?

2 Likes

^ having the same issue

Runtime is not quite up to date with uGUI. EventSystem.IsPointerOverGameObject() doesn’t have an equivalent yet. Right now you have to roll your own solution of getting all VisualElements and see if the mouse is over any of those.

1 Like

In playing around with this, I wasn’t sure how to tell if the mouse was over an individual VisualElement. I ended up using a boolean in my UI manager class that resets to false at the end of each update, and get’s set to true in any of my clicked event functions. This then let me reference the boolean to check if the click was already handled. Not ideal, but it will work until there is something more purpose build from Unity.

1 Like

Any update on if there is any Unity development to better handle this? This is a pretty essential function required for runtime usage of UI Toolkit.

As expected my workaround is not 100% effective. There are scenarios where the Input.GetMouseButtonUp(0) registers on a Update(), and the UI clicked callback is executing on the following frame. Before I invest in building some messy logic where on a mouse click I wait a few frames to ensure no UI click set’s my boolean before completing the click action, I want to check that there isn’t plans to develop anything on the Unity side in the not to distant future.

Well I implemented my multi-frame check as a work around. Even waiting adding a single frame delay in registering the click is making input feel more sluggish.

Again, I recommend the Unity development team address this as it’s pretty fundamental to the real-time use of the UIToolkit.

1 Like

Just checking once again if there planned improvement to support runtime use of UITK and avoid click-thoughs. This is pretty fundamental for UI in games… Am I missing something? I’m surprised this isn’t brought up more.

My workaround randomly/rarely fails as it takes multiple frames before the callback is executed.

1 Like

It’s coming, we have an implementation that will work for 2021.1+, it’s just not out yet.

2 Likes

Fantastic news! Thank you for the update!

Hello there, do you have news about this please? This missing feature is proving to be tricky, having to implement a wrapper visual element everywhere would take a lot of time and effort.

Also, please do consider the condition where you have an invisible container, that shouldn’t count as a pointer over ui, or make it an option.

Thanks!

We’re in the process of getting it published, should be out very soon hopefully! :slight_smile:

1 Like

Wonderful, thanks!

UPDATE: sry don’t mind my stupidity :smile:

The “old” but gold way to do it still works with UIToolkit :slight_smile:

EventSystem.current.IsPointerOverGameObject()

GLHF :wink:

This returns always true in my case, except when the cursor is outside editor window.
Could it be, that it’s related to the fact, that the PanelSettings span the entire screen?

1 Like

Any news on that, half a year later?

Just add panels inside your UI with visibility set to hidden and you will be able to click through those.

It’s already public, with Unity 2021.2 if I’m not mistaken. You just need an EventSystem component on your scene in order for your UGUI and UI Toolkit UIs to play nicely together, and make sure your Canvas and PanelSettings sort orders are at the expected position.

I figured it out, it returned the expected value. What I didn’t know was, that if you have Colliders in your scene with
IPointerEnterHandler/IPointerExitHandler, the colliders also count as UI.

1 Like

Got the same problem. Added Eventsystem component but still nothing. any way to get a link to the relevant article/patch notes?

@YodaDoge , use EventSystem.current.IsPointerOverGameObject() to prevent click throughs (PC/Editor). On my side there were some trobules if I provide pointer index.

Contrary to PC - you should provide fingerId on mobile devices

var touch = UnityEngine.Input.GetTouch(0);
EventSystem.current.IsPointerOverGameObject(touch.fingerId)