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?
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.
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.
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.
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.
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.
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?
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.
@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)