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

Styling the scrollbar of a scrollview

Discussion in 'UI Toolkit' started by UniZoja, Sep 23, 2021.

  1. UniZoja

    UniZoja

    Joined:
    Oct 1, 2018
    Posts:
    30
    Sorry for such a simple question, but google gives me mostly results for older versions of unity which do not use the UI Builder.

    I want to change the scroller low and high button and the slider width.

    I see the elements in the hierarchy but they are greyed out for some reason (see image).

    scroller.png
     
  2. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    They are greyed out because the Scroller instantiates them as sub-controls. They are not visible in your UXML file hierarchy, but here in the builder to show they are created.

    When you select these readonly elements in the Builder, you can see in the Inspector, under StyleSheet, what style classes are matching/applied to them. You can use these classes in your stylesheets to override any default styling.

    In the case of low/high buttons, you could for example use the classes named
    .unity-scroller__slider
    .unity-scroller__low-button
    and
    .unity-scroller__high-button
    . You can also use the name of the element in your stylesheet, ex.
    #unity-low-button
     
    Last edited: Sep 24, 2021
    squigglebucket and UniZoja like this.
  3. Lupus_Solus

    Lupus_Solus

    Joined:
    Jul 14, 2013
    Posts:
    7
    Perfect - Thank-you
     
  4. qwert024

    qwert024

    Joined:
    Oct 21, 2015
    Posts:
    43
    Hi!
    I was wondering how we can have several different styles for scrollers.
    We want several scrollers with different looking in our game.
    We can overide the default selector's styling, but it applies to all scrollers in game because they are all using the the default selector.
    However, the settings are all greyed out and we can't apply our own selectors to it.
    It would be awesome if anyone could help!
    upload_2022-1-28_16-37-29.png
     
  5. vejab

    vejab

    Joined:
    Dec 21, 2021
    Posts:
    85
  6. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    Exactly, the descendant or child selector should work.
    Names would work, or you can have different class names to add to your ScrollView, for the various scroller types, and use descendant or child selector to reach the scroller styling in uss.
     
  7. qwert024

    qwert024

    Joined:
    Oct 21, 2015
    Posts:
    43
    Hi, thank you for your reply!
    It would be wonderful if you would help elaborate or share some examples.
    This is what i've tried: I add the code below in my uss file
    Screenshot 2022-02-08 172302.png

    In the Builder window, I still can't apply my class to it
    upload_2022-2-8_17-36-3.png

    Then I've also tried
    Code (CSharp):
    1. highBtn.AddToClassList("my-scroller__high-button");
    but it didn't work either.
    I think I am somewhat missing some basic understanding of how it works:(
     

    Attached Files:

    Last edited: Feb 8, 2022
  8. vejab

    vejab

    Joined:
    Dec 21, 2021
    Posts:
    85
    #ScrollView1 .unity-scroller__high-button {
    • name of the scrollview (the point in giving it a name was to use it :))
    • space for the descendant selector
    • class of the item you want to edit

    If you want to modify two things, you can do this I think (I'm not sure)
    #ScrollView1 .unity-scroller__high-button, #ScrollView1 .unity-scroller__low-button {
     
  9. qwert024

    qwert024

    Joined:
    Oct 21, 2015
    Posts:
    43
    Thank you very much!!!
    Finally I figured it out!
    Say I have a Scrollview called #test-scrollview, and I want to change the style of its horizontal scroller's dragger.
    I can use descendant selector ("Any descendants of the first selector, at any level, that also match the second selector.") to achieve that.
    Since .unity-scroller--horizontal and .unity-base-slider__dragger are already the dragger's selector,
    I can use the code below in .USS to control its style, without affecting other scrollviews' draggers in my game.
    Code (CSharp):
    1. #test-scrollview .unity-scroller--horizontal .unity-base-slider__dragger {
    2.     width: 45%;
    3.     align-self: center;
    4.     background-color: rgba(159, 159, 225, 0.8);
    5.     border-top-left-radius: 8px;
    6.     border-bottom-left-radius: 8px;
    7.     border-top-right-radius: 8px;
    8.     border-bottom-right-radius: 8px;
    9. }
    Screenshot 2022-02-19 172410.png
     
    Max_Aigner and vejab like this.
  10. richardNXRT

    richardNXRT

    Joined:
    Aug 4, 2022
    Posts:
    26
    Damn , the old canvas system was so much easier to use. This UI.toolkits is way more time consuming and complex to use, for marginal improvements
     
    Neuro2019 and Squitz like this.
  11. dmitro1982

    dmitro1982

    Joined:
    Sep 20, 2018
    Posts:
    6
    I am web developer, but USS in Unity is real nightmare to styling. It only looks like normal web elements and CSS but in reality it much harder to use, most of the time behavior of Unity VisualElements unpredictable, usual tricks from web dev are not working.
     
  12. C-UITools

    C-UITools

    Unity Technologies

    Joined:
    Jun 23, 2021
    Posts:
    22
    Hi @dmitro1982,

    Thank you for your message. Did you reply to this specific thread because you had an issue with styling the scrollbar? Were you able to style it?
     
  13. dmitro1982

    dmitro1982

    Joined:
    Sep 20, 2018
    Posts:
    6
    Yes, all my attempts to apply USS to the scroll bar failed, for example if make UP/DOWN buttons smaller, the bar is still the same size and scrollbar got gaps. Such behavior is not expected, all web components will expand and fill gaps, but not in Unity.

    Not related to scroll bar behavior:
    - Empty class - failed compilation. Why? Empty classes is 100% ok in CSS.
    - any wrong property - will break gui. In normal CSS wrong properties just skipped and never break UI.
     
  14. laila-chammaa

    laila-chammaa

    Unity Technologies

    Joined:
    May 10, 2022
    Posts:
    28
    Hello! That's strange, these cases (empty class, wrong property) in a USS file should only produce warnings. I cannot reproduce on the latest version. Which version are you using?