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

Hide scrollbar

Discussion in 'UGUI & TextMesh Pro' started by Essential, Dec 16, 2014.

  1. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    I want to hide a scrollbar if the content of its UI Scroll Rect fits fully and doesn't need scrolling. I notice this doesn't happen automatically.

    Disabling the scrollbar is quite a simple task, but how can I detect when to hide it?
     
  2. highlyinteractive

    highlyinteractive

    Joined:
    Sep 6, 2012
    Posts:
    116
    I normally compare the dimensions of the mask and the scroll content using the sizeDelta of the respective rectTransform's
     
  3. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    I created this script that will autohide the scrollbar. Just attach it to a Scroll Rect gameobject.

    Code (CSharp):
    1. // Attach this script to the ScrollRect
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. [RequireComponent (typeof (ScrollRect))]
    7. public class AutoHideUIScrollbar : MonoBehaviour {
    8.  
    9.     public bool alsoDisableScrolling;
    10.  
    11.     private float disableRange = 0.99f;
    12.     private ScrollRect scrollRect;
    13.     private ScrollbarClass scrollbarVertical = null;
    14.     private ScrollbarClass scrollbarHorizontal = null;
    15.  
    16.     private class ScrollbarClass
    17.     {
    18.         public Scrollbar bar;
    19.         public bool active;
    20.     }
    21.  
    22.  
    23.     void Start ()
    24.     {
    25.         scrollRect = gameObject.GetComponent < ScrollRect >();
    26.         if ( scrollRect.verticalScrollbar != null )
    27.             scrollbarVertical = new ScrollbarClass() { bar = scrollRect.verticalScrollbar, active = true };
    28.         if ( scrollRect.horizontalScrollbar != null )
    29.         scrollbarHorizontal = new ScrollbarClass() { bar = scrollRect.horizontalScrollbar, active = true };
    30.  
    31.         if ( scrollbarVertical == null && scrollbarHorizontal == null )
    32.             Debug.LogWarning ("Must have a horizontal or vertical scrollbar attached to the Scroll Rect for AutoHideUIScrollbar to work");
    33.     }
    34.  
    35.     void Update ()
    36.     {
    37.         if ( scrollbarVertical != null )
    38.             SetScrollBar( scrollbarVertical, true );
    39.         if ( scrollbarHorizontal != null )
    40.             SetScrollBar( scrollbarHorizontal, false );
    41.     }
    42.  
    43.     void SetScrollBar ( ScrollbarClass scrollbar, bool vertical )
    44.     {
    45.         if ( scrollbar.active && scrollbar.bar.size > disableRange )
    46.             SetBar( scrollbar, false, vertical );
    47.         else if ( !scrollbar.active && scrollbar.bar.size < disableRange )
    48.             SetBar( scrollbar, true, vertical );
    49.     }
    50.  
    51.     void SetBar ( ScrollbarClass scrollbar, bool active, bool vertical )
    52.     {
    53.         scrollbar.bar.gameObject.SetActive( active );
    54.         scrollbar.active = active;
    55.         if ( alsoDisableScrolling )
    56.         {
    57.             if ( vertical )
    58.                 scrollRect.vertical = active;
    59.             else
    60.                 scrollRect.horizontal = active;
    61.         }
    62.     }
    63. }
     
    Last edited: Dec 16, 2014
  4. puzzlekings

    puzzlekings

    Joined:
    Sep 6, 2012
    Posts:
    402
    Hi @Essential

    Just wanted to say a big thanks for this as I was searching round for the best way to do this and came across your script. Really it should be part of the default implementation.

    You're a star!

    Thank you.

    Nalin
     
    Essential likes this.
  5. DrakharStudio

    DrakharStudio

    Joined:
    Mar 2, 2012
    Posts:
    8
    Thanks a lot for this! As @puzzlekings said, it should be part of the default implementation
     
  6. speedacidrain

    speedacidrain

    Joined:
    May 4, 2015
    Posts:
    6
    Thanks!!!!!!!!!! ^_^
     
  7. NewYorkHOK

    NewYorkHOK

    Joined:
    Oct 13, 2017
    Posts:
    5
  8. EyeDev44

    EyeDev44

    Joined:
    Apr 8, 2017
    Posts:
    140
    Not working at all.
     
  9. Francesco-FL

    Francesco-FL

    Joined:
    May 25, 2021
    Posts:
    176
    It was 2014, maybe something has changed. Have you tried selecting the "Auto Hide" option in the scroll-rect?

    Immagine 2021-08-29 155205.png
     
  10. richerf3212

    richerf3212

    Joined:
    May 25, 2022
    Posts:
    3
    The scrollbar and scrollview prefabs have a "normal color" property where you can assign its alpha value to 0, making it transparent.
     

    Attached Files:

    wzkiro likes this.
  11. Iam01c

    Iam01c

    Joined:
    Nov 14, 2015
    Posts:
    2
    If anyone happens to find this much later like I did (top results on google).
    In my example there is no scroll rect, but I believe it should work if there is as well.

    Attach the script to any scrollbars you want to hide when they are full.
    Tested and works in Unity 2021.3.9f1.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. [RequireComponent(typeof(Scrollbar))]
    6. public class AutoHideScrollbar : MonoBehaviour
    7. {
    8.     private Scrollbar scrollbar;
    9.  
    10.     private void Start()
    11.     {
    12.         scrollbar = GetComponent<Scrollbar>();
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         scrollbar.targetGraphic.gameObject.SetActive(scrollbar.size < 1f);
    18.     }
    19. }
    20.