Search Unity

Question [Runtime] Changing selection color on a ListView

Discussion in 'UI Toolkit' started by chris-z562, Aug 31, 2020.

  1. chris-z562

    chris-z562

    Joined:
    Jul 11, 2012
    Posts:
    41
    Bad solution: overwrite the background color of each selected item in the ListView. Then I also have to manage when an item is hovered (and change color accordingly). That's messy and redoes the job ListView is already doing.

    What's a better way to do it? Can I change a StyleSheet linked to the items? Should I extend ListView somehow?

    Thanks!
     
    Last edited: Aug 31, 2020
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi chris-z562,

    The recommend way is to use USS to override the default ListView styles.
    For example if you would want the selection color to be yellow you can do :

    Code (CSharp):
    1. .unity-list-view__item--selected {
    2.     background-color: yellow;
    3. }
    You can inspect the ListView elements with the UI Toolkit debugger to see all possibilities.
     
    salah-rashad and chris-z562 like this.
  3. chris-z562

    chris-z562

    Joined:
    Jul 11, 2012
    Posts:
    41
  4. chris-z562

    chris-z562

    Joined:
    Jul 11, 2012
    Posts:
    41
    @jonathanma_unity I found two other selectors:

    .unity-list-view__item--alternative-background

    .unity-list-view__item--drag-hover


    Changing their background color did nothing. The code you provided did work. Am I missing something?

    Also, I dug into
    DefaultCommonDark_inter.uss.asset
    to find those selectors. Is there a way to see them in the UITK Debugger?
     
    Last edited: Sep 4, 2020
  5. chris-z562

    chris-z562

    Joined:
    Jul 11, 2012
    Posts:
    41
    Alright, I figured it out.

    Code (CSharp):
    1. .unity-list-view__item {
    2.     background-color: red;
    3. }
    4.  
    5. .unity-list-view__item:hover {
    6.     background-color: blue;
    7. }
    8.  
    9. .unity-list-view__item--selected {
    10.     background-color: yellow;
    11. }
    12.  
    13. .unity-list-view__item--selected:hover {
    14.     background-color: green;
    15. }
    For anyone stumbling upon this: The 'selected' selectors have to be after the normal ones. Otherwise they are ignored/overwritten by the others.
     
    Last edited: Sep 4, 2020
    yukunlinykl and salah-rashad like this.
  6. chris-z562

    chris-z562

    Joined:
    Jul 11, 2012
    Posts:
    41
    @jonathanma_unity When I select an item in the list and then select something from another list I'd like the first selection to fade out a bit (by changing the selection color from blue to grey). So I can't have "active" selections in several places. Is there a good way to do this currently?

    First idea is to change the USS at runtime, but I don't think that's possible? And neither is swapping USS files?
     
  7. yukunlinykl

    yukunlinykl

    Joined:
    Dec 17, 2021
    Posts:
    21

    I'm using Unity 2022.1.0b11.2843
    I try your script but "--selected" and "--selected:hover" don't work.

    and I found out this is OK for me:

    .unity-list-view__item:selected {
    color: blue;
    background-color: yellow;
    }

    .unity-list-view__item:selected:hover {
    color: red;
    background-color: green;
    }

    Just change --selected to :selected.

    But according to https://docs.unity3d.com/2022.2/Documentation/Manual/UIE-USS-Selectors.html
    ":selected N/A. Unity does not use this pseudo-state."
    I think the document is lack of updates.
     
  8. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi, I looked at the code and the :selected pseudo-state is not officially supported but is still available for backward compatibility. :selected was replaced with :checked. I suggest you use that instead if you can.
     
    yukunlinykl and Onigiri like this.
  9. yukunlinykl

    yukunlinykl

    Joined:
    Dec 17, 2021
    Posts:
    21
    Thank you! :checked works for me
    I hope if the document:

    selected: N/A. Unity does not use this pseudo-state.


    declare this point will be better, for example:

    selected: N/A. Unity does not use this pseudo-state. use :checked instead.
     
  10. pokelocos

    pokelocos

    Joined:
    Nov 9, 2015
    Posts:
    54
    How can I achieve this through C# code? I need to make these changes, but I cannot do it through styles.thanks
     
  11. cpalma-unity

    cpalma-unity

    Unity Technologies

    Joined:
    Nov 30, 2020
    Posts:
    110
    There is no way to set styles to a specific pseudo-class by code right now.

    Something you can do is to listen to MouseEnterEvent and MouseLeaveEvent to detect hover and use the selectionChanged callback in list view to react to the selection changes.