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

Question Mouse enter/leave events not firing when exiting game window

Discussion in 'UI Toolkit' started by JasonBricco, Feb 9, 2022.

  1. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Is there any reason that the MouseEnterEvent and MouseLeaveEvent do not work correctly when the game window is exited (both in the editor and in a build)?

    Let's say I have a panel along the top edge of the screen. I register MouseEnterEvent for it and set a bool to true when the mouse enters, and to false when it exits via MouseLeaveEvent.

    If I move the cursor out of the top of the window, the leave event doesn't fire. Now, if I enter the mouse into the window anywhere else, my bool will be true and the system thinks the mouse is over the top panel when it is not.

    As is, to work around it I think I have to do a Rect.Contains check using the worldBound value of my panel. However, even this isn't enough since Input.mousePosition's y value is flipped from the UI system's y value. It would be much cleaner if the events would work correctly.

    Or is there some better way to handle this that I don't know of?

    Edit: this is even more obvious when placing a standard button on the edge of the window. The button stays in its hovered state when the mouse exits the screen and when it comes back in away from the button.

    Unity version: 2021.2.10.
     
    Last edited: Feb 9, 2022
  2. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    There are also no pseudo-state changes triggered when you exit the game window, aka a the :hover state of a button will still be active.
     
  3. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    So maybe that's a separate problem from the one I originally discussed, even though it seems like the pseudo-state changes would be based on events and events are not firing when leaving the window.

    Regardless, it is clearly undesirable behavior - there shouldn't be a case where a button remains visually hovered while the cursor isn't over it. It looks bad.

    (Not considering navigation events, just basic functionality.)
     
  4. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    696
    We believe we have a fix for this issue that is currently waiting to be published in 2021.2, in the same batch as this one: https://issuetracker.unity3d.com/is...ed-the-transition-is-swiped-off-of-the-object

    This is not exactly the same case, but it's in the same batch of fixes and the specific issue you're having is not in a public ticket :oops:

    Please watch the status of that one, and when it's out for 2021.2 if you're still having issues let us know ;)
     
    JasonBricco likes this.