Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved .unity-button:hover backgorund color is ignored in theme style sheets

Discussion in 'UI Toolkit' started by Riki9811, May 6, 2024.

  1. Riki9811

    Riki9811

    Joined:
    Jan 14, 2016
    Posts:
    2
    I want to change the styling of every button in my game, and for what I understand something like this is meant to be done with TSS theme files.

    I've created this ButtonStyle.uss stylesheet:
    Code (USS):
    1. .unity-button {
    2.     background-color: red;
    3.     transition-property: scale;
    4.     transition-duration: 0.25s;
    5. }
    6.  
    7. .unity-button:hover {
    8.     background-color: green;
    9.     scale: 1.1 1.1;
    10. }

    And I've linked this file inside my UnityDefaultRuntimeTheme.tss like this:

    Default theme using StyleSheets.png

    Now the default background-color of the button is red as it should be, but when hovered the button turns white instead of green. The weired thing is that the button DOES scale up on hover, so the second rule is being applied to the button.

    Button behaviour.gif

    This also happens in a brand new project. What I believe is happening is that for some reason the background-color inside my rule is being overridden by the white-ish color of an hovered button from the default theme.

    The way I can 'fix' this is by increasing the specificity of the rule by setting the selector to something like:

    Button.unity-button.unity-text-element:hover


    But this feels extremely clunky and not really feasable in a big project where you need to override lots of other styles. Plus I feel like even if there is a default unity-button:hover rule mine should override it as the docs say:
    Is this a bug or am i missing something?
    Unity version: 6000.0.0 Beta 16
     
  2. FreddyC-Unity

    FreddyC-Unity

    Unity Technologies

    Joined:
    Jun 9, 2021
    Posts:
    46
    Hi!

    It seems like you're just missing the :enabled pseudo-class:

    Code (USS):
    1. .unity-button:hover:enabled {
    2.     background-color: green;
    3.     scale: 1.1 1.1;
    4. }
    Our default style has a background-color style applied under .unity-button:hover:enabled but not a scale, which explains why scale was applied.

    For these situations, you can use the UI Toolkit Debugger and see which matching selector is taking precedence.

    Hope this helps!
     
    VenetianFox likes this.
  3. Riki9811

    Riki9811

    Joined:
    Jan 14, 2016
    Posts:
    2
    First of all thanks a lot because :enabled was indeed the missing piece.

    Regarding your advice to use the UI Toolkit Debugger: the 'Matching Selectors' doesn't show me rules with pseudo-classes.

    Debugger doesn't show Pseudo-class selectors.gif

    The only thing I see is the 'Pseudo States' label, but that wouldn't have helped me since it doesn't show the :enabled pseudo-class. Is there an option to toggle pseudo-class selectors on/off?
     
    VenetianFox likes this.
  4. FreddyC-Unity

    FreddyC-Unity

    Unity Technologies

    Joined:
    Jun 9, 2021
    Posts:
    46
    Glad it helped!

    And regarding the debugger, it seems like a bug. In this specific case, the matching selectors list should have your pseudo selector(s), along with the runtime's pseudo selector(s), as soon as the element's pseudo state updates. You may report it using the report a bug window (help > Report a bug...). Thank you in advance!