Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Pseudo-class :hover doesn't seem to work

Discussion in 'UI Toolkit' started by MetalFox, May 16, 2021.

  1. MetalFox

    MetalFox

    Joined:
    May 14, 2014
    Posts:
    10
    I don't understand how to make the pseudo-class :hover work. I have a button with a background-color that I want to change on hover, so I have an USS file with this code (heavily inspired from the docs : https://docs.unity3d.com/Manual/UIE-USS-Selectors-Pseudo-Classes.html) :

    Code (CSharp):
    1. Button {
    2.     background-color: yellow;
    3. }
    4.  
    5. Button:hover {
    6.     background-color: palegreen;
    7. }
    8.  
    And I import it in my component (it's the "test.uss") :

    upload_2021-5-16_2-16-25.png

    The background of the button is indeed yellow (it's not an inline style : when I remove the USS file, it goes back to its default color), but the color never changes on hover (neither in the editor, nor in game). In the opposite, if I add an :active pseudo-class, it works perfectly, only :hover causes issues.

    I tried to change different properties on hover (width, border-color...), to add classes (that's where I had the problem first) and I even tried other elements (changing the background of VisualElements or Toggles), but nothing seems to work.

    I use the latest version of the UI Toolkit so far (preview 14). I don't know if it's important, but I also use the new Input System.

    Is there some configuration to do in order to detect hovering in USS ?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    Nothing extraordinary:
    screenshot1.png screenshot2.png

    Both screenshots were made after hitting the "Preview" button in the top-right corner. The second when I was hovering the mouse, the first when I was not hovering the mouse.
     
  3. MetalFox

    MetalFox

    Joined:
    May 14, 2014
    Posts:
    10
    Thank you for your input, and let me correct myself a bit : it works well in preview, but doesn't work in-game, when I press the "play" button. Here is in the preview :

    Not hovered :
    upload_2021-5-16_16-19-33.png

    Hovered :
    upload_2021-5-16_16-19-49.png


    And here is the same UI displayed in game (by pressing "play" in the editor, but building the game does the same thing).

    Not hovered :
    upload_2021-5-16_16-20-41.png

    Hovered :
    upload_2021-5-16_16-21-1.png

    Maybe it's in-game the implementation that is bad, so here are more details.

    upload_2021-5-16_16-30-1.png

    I have a GameObject with a UIDocument pointing to my UI (it's the "Seller" Visual Tree Asset). This GameObject is disabled by default, and is enabled when the character press the action key near an NPC.

    I tried disabling all the other scripts linked to the GameObject (just in case), but it made no difference.

    Just to add some little things that could help :
    - There are no errors in the console
    - Adding an :active pseudo-class to change the background works well, either in the preview or in the game
    - I tried moving my GameObject at the root level of the scene, or as a children of my "SellerNPC" GameObject, but it made no difference
    - I tried enabling the UI GameObject by default, but it didn't work
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    IDK, it works for me, this is in play mode, but also in build (my mouse was hovering over the "Credits" button):
    screenshot1.png screenshot2.png

    UI Toolkit 1.0 - preview14.
     
  5. MetalFox

    MetalFox

    Joined:
    May 14, 2014
    Posts:
    10
    I tested this in a new project from scratch and it works well, so I guess there is some black magic, somewhere in the code, that creates some kind of conflicts. I'll double check everything to see what is happening >.>
     
  6. hunterofakara

    hunterofakara

    Joined:
    Oct 4, 2018
    Posts:
    21
    I've had the exact same problem after swapping to the new package based input system. In addition to the hover style, the MouseOverEvent doesn't seem to trigger in play mode so I couldn't use that as a workaround.

    I also can't seem to get a sensible current mouse position from the new inputsystem, so I've been assuming it's related in some way.

    ( Mouse.current.position.ReadValue() is unset for the first click etc event if the cursor hasn't moved since recompile/game load and seems to also report an incorrect or outdated position if I move the position via external functions.
    UnityEngine.Input.mousePosition always used to report correctly in these situations. Now I'm resorting to using OnGUI Event.current.mousePosition to get a baseline position and Mouse.current.delta.ReadValue() to track movement. )

    Windows 10
    Unity 2021.1.5f1
    UI Builder/Toolkit packages 1.0.0-preview.14
    Input System 1.0.2
     
  7. Daxten

    Daxten

    Joined:
    Sep 20, 2013
    Posts:
    30
    reviving this thread:

    Can confirm, pseudo classes (at least :hover) do not work with new input system
     
  8. KamilCSPS

    KamilCSPS

    Joined:
    May 21, 2020
    Posts:
    376
  9. Daxten

    Daxten

    Joined:
    Sep 20, 2013
    Posts:
    30
    yes just checked, new input system is setuped (i think otherwise it doesnt work at all)
     
  10. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    323
    Are you using multiple panels (i.e. multiple ui documents with different panel settings)?
     
  11. KazuyaNoguchi_at_DispatchedCompanyAccount

    KazuyaNoguchi_at_DispatchedCompanyAccount

    Joined:
    Apr 9, 2021
    Posts:
    1
    If the hover does not work properly for multiple UI documents with different panel settings, changing the Sort Order value of the non-working panel may make it work. In my case, I was using two panel settings and had a bug where the Pseudo status went to None immediately after hovering. After changing the Sort Order in the panel settings of the buggy UI document from 0 to -1, the hover state is now stable.
     
  12. Dy4848

    Dy4848

    Joined:
    Jun 1, 2021
    Posts:
    1
    I had a problem similar to this solved by deleting all the previous styles and CSS files, then creating all the styles again.
     
  13. Dubitrubi

    Dubitrubi

    Joined:
    Jan 31, 2020
    Posts:
    5
    Had this problem as well using the new inupt system ui input module i used for my old canvas system. Switching now to UIElements I had to disable the EventSystem and InputSystemUIInputModule Components, then the new UI seems to work as intended.

    You may use that as a workaround as you can enable/disable those components on runtime (only tested in editor game mode yet).

    upload_2022-3-2_15-14-42.png

    Disabling those components:
    - Mouse Events for Canvas Elements disabled
    - Mouse Events for UIElements enabled

    Enabling this components:
    - Mouse Events for Canvas Elements enabled
    - Mouse Events for UIElements disabled
     
  14. mashermack

    mashermack

    Joined:
    Mar 14, 2019
    Posts:
    2
    I know it's an old thread but it's the top-most Google search result.

    If you end up here probably you have "Simulate touch input" checked on the options inside Input debug pane. This will simulate touch screens which have no :hover pseudo capability.

    upload_2023-9-20_10-13-34.png

    Event system and input system need to be on both for me, otherwise the UIDocument wont work.