Search Unity

Hide ScrollBar when not scrolling

Discussion in 'UI Toolkit' started by areavisuale, Jun 9, 2021.

  1. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    I would like to obtain the same effect of many mobile apps, like google chrome, in which if you scroll a page the scrollbar disappear after the page stopped from scrolling and reappear immediately when my finger touch again the page.
    I’m using a ScrollView with inertia deceleration, and I try to detect when velocity=0 to hide my scrollbar, but for some reason the velocity at the top and bottom of my scrollview doesn’t stop at zero everytime. I tried also to compare the velocity over time but without success. Have you a light solution for this?
    thanks!
     
  2. cpalma-unity

    cpalma-unity

    Unity Technologies

    Joined:
    Nov 30, 2020
    Posts:
    110
    Hi. You could use the
    ChangeEvent
    of the scroller so you can keep track of when the scroll changed. Something like:
    Code (CSharp):
    1.  
    2. private ScrollView m_ScrollView;
    3.  
    4. public void CreateGUI()
    5. {
    6.     // Import UXML
    7.     ...
    8.  
    9.     m_ScrollView = rootVisualElement.Q<ScrollView>();
    10.     m_ScrollView.verticalScroller.RegisterCallback<ChangeEvent<float>>(OnScrollerValueChanged);
    11. }
    12.  
    13. private void OnScrollerValueChanged(ChangeEvent<float> evt)
    14. {
    15.     // Time.time if you are in runtime
    16.     m_LastScrollTime = EditorApplication.timeSinceStartup;
    17. }
    18.  
    19. private void Update()
    20. {
    21.     if (EditorApplication.timeSinceStartup - m_LastScrollTime > 1f)
    22.     {
    23.         m_ScrollView.verticalScroller.visible = false;
    24.     }
    25.     else
    26.     {
    27.         m_ScrollView.verticalScroller.visible = true;
    28.     }
    29. }
    30.  
    Like this you could show the scroll when starts moving and hide it when it hasn't move in certain time like Chrome does.
     
  3. areavisuale

    areavisuale

    Joined:
    Jul 23, 2015
    Posts:
    60
    sorry but I don't understand what is UXML, could you please translate your code using normal scrollrect/scrollbar components? thank you
     
  4. cpalma-unity

    cpalma-unity

    Unity Technologies

    Joined:
    Nov 30, 2020
    Posts:
    110