Search Unity

Bug Button hovered state remains when hiding/showing parent

Discussion in 'UI Toolkit' started by JasonBricco, Jan 29, 2022.

  1. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Since this seems quite basic, I may have missed something. I've found it quite challenging to find information about UI Toolkit from searches.

    I have a button that is set to make its parent element (panel) go away using display = DisplayStyle.None. If I bring back the panel while my cursor isn't hovering over any UI element (for example because a keyboard key brings the panel back), the button that was pressed remains in the hovered state until the cursor enters a UI element again.

    Is there a way to stop this? Is it a bug? Things I've tried that did not work:
    • Deferring panel hiding to the end of frame/next frame.
    • Hiding it by setting opacity to 0 (works, but then the panel still blocks the cursor).
    • Setting opacity to 0 and moving it off screen with translate.
    • Setting opacity to 0 and deferring the movement off screen using a coroutine (occasionally worked but occasionally didn't regardless of the number of frames used).
    • Setting opacity to 0 and setting focusable to false.
    I thought about simply forcing the button state to change to not be hovered, but I haven't found a way to do this. That also seems quite hacky.

    Unity version: 2021.2.9.
     
  2. It works for me. Here is what I have done:
    - built a UI quickly:
    screenshot_builder.png

    - Setup in Unity

    Quick code to handle stuff:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UIElements;
    3.  
    4. public class Test : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private UIDocument uiDocument;
    8.  
    9.     private VisualElement _root;
    10.     private Button _button;
    11.     private VisualElement _panel;
    12.  
    13.     private void Awake()
    14.     {
    15.         _root = uiDocument.rootVisualElement;
    16.         _button = _root.Q<Button>("btn");
    17.         _panel = _root.Q<VisualElement>("panel");
    18.      
    19.         _button.clickable.clicked += TogglePanel;
    20.     }
    21.  
    22.     private void TogglePanel() => _panel.style.display = DisplayStyle.None;
    23.  
    24.     private void Update()
    25.     {
    26.         if (Input.GetKeyUp(KeyCode.A))
    27.         {
    28.             _panel.style.display = DisplayStyle.Flex;
    29.         }
    30.     }
    31. }
    - Run and results:
    screenshot.png screenshot1.png screenshot2.png screenshot3.png
    In order left to right: normal, hover, click, bring back via 'A' key (see code above) without hovering the button.
     
  3. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Is your cursor hovering a UI element when bringing the panel back, for example the red area? Because it looks like it's taking up the whole area. It also works for me if any UI element is hovered, but not if none are.
     
  4. No, sorry, that's just my clumsy snip-it technique. The panel in reality has 75% height.
    screenshot.png

    My cursor was in the grey area, which is part of the "skybox" in a standard 3D project. So basically outside of any visible UI element. Also same if my cursor isn't even over the game window. And also Unity 2021.2.9f1, obviously with the built-in UI Builder and UI Toolkit.
     
  5. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    I recreated this in my testing project so it would be separate from my existing setup, and I think I figured out the part causing it.

    I have an event system in the scene from UGUI. It seems when I remove that and use the default event system from UI Toolkit, the problem doesn't happen.

    However, then I can't use EventSystem.current.IsPointerOverGameObject() to detect if the cursor is over a UI element. I need to be able to do this, and so it would be nice if this problem didn't happen with the UGUI event system.
     
    Last edited: Jan 29, 2022
    Lurking-Ninja likes this.
  6. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Still broken in 2021.2.10.
     
  7. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    575
    There were two patches that landed recently in the 2022.1 alpha that seems to solve the issue, it will take a moment for them to be backported to the older version.
     
    JasonBricco likes this.