Search Unity

Hide scrollbar

Discussion in 'Unity UI (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:
    92
    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:
    377
    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:
    4
    Thanks!!!!!!!!!! ^_^
     
  7. NewYorkHOK

    NewYorkHOK

    Joined:
    Oct 13, 2017
    Posts:
    5