Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'UI Toolkit' started by skowroshima, Sep 20, 2020.

  1. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    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?
     
    RKar and Lau-ried like this.
  2. Lau-ried

    Lau-ried

    Joined:
    May 24, 2019
    Posts:
    1
    ^ having the same issue
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    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.
     
    Lau-ried likes this.
  4. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    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.
     
    Chris-Trueman likes this.
  5. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    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.
     
  6. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    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.
     
    RKar likes this.
  7. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    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.
     
    Last edited: Feb 11, 2021
    RKar likes this.
  8. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    696
    It's coming, we have an implementation that will work for 2021.1+, it's just not out yet.
     
    RKar and MousePods like this.
  9. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    Fantastic news! Thank you for the update!
     
  10. PaoloAm

    PaoloAm

    Joined:
    Jul 15, 2019
    Posts:
    28
    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!
     
  11. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    696
    We're in the process of getting it published, should be out very soon hopefully! :)
     
    Onigiri likes this.
  12. PaoloAm

    PaoloAm

    Joined:
    Jul 15, 2019
    Posts:
    28
    Wonderful, thanks!
     
  13. mrSaig

    mrSaig

    Joined:
    Jan 14, 2010
    Posts:
    68
    UPDATE: sry don't mind my stupidity :D

    The "old" but gold way to do it still works with UIToolkit :)

    Code (CSharp):
    1. EventSystem.current.IsPointerOverGameObject()
    GLHF ;)
     
    Last edited: Nov 11, 2021
  14. magnetic_scho

    magnetic_scho

    Joined:
    Feb 2, 2020
    Posts:
    92
    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?
     
    viktorcode likes this.
  15. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Any news on that, half a year later?
     
  16. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    Just add panels inside your UI with visibility set to hidden and you will be able to click through those.
     
  17. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    696
    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.
     
  18. magnetic_scho

    magnetic_scho

    Joined:
    Feb 2, 2020
    Posts:
    92
    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.
     
    viktorcode likes this.
  19. YodaDoge

    YodaDoge

    Joined:
    Oct 2, 2018
    Posts:
    1
    Got the same problem. Added Eventsystem component but still nothing. any way to get a link to the relevant article/patch notes?
     
  20. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    123
    @YodaDoge, use
    Code (csharp):
    1. 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
    Code (csharp):
    1.  
    2. var touch = UnityEngine.Input.GetTouch(0);
    3. EventSystem.current.IsPointerOverGameObject(touch.fingerId)
    4.