Hide scrollbar

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?

I normally compare the dimensions of the mask and the scroll content using the sizeDelta of the respective rectTransform’s

I created this script that will autohide the scrollbar. Just attach it to a Scroll Rect gameobject.

// Attach this script to the ScrollRect
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

[RequireComponent (typeof (ScrollRect))]
public class AutoHideUIScrollbar : MonoBehaviour {
 
    public bool alsoDisableScrolling;

    private float disableRange = 0.99f;
    private ScrollRect scrollRect;
    private ScrollbarClass scrollbarVertical = null;
    private ScrollbarClass scrollbarHorizontal = null;

    private class ScrollbarClass
    {
        public Scrollbar bar;
        public bool active;
    }


    void Start ()
    {
        scrollRect = gameObject.GetComponent < ScrollRect >();
        if ( scrollRect.verticalScrollbar != null )
            scrollbarVertical = new ScrollbarClass() { bar = scrollRect.verticalScrollbar, active = true };
        if ( scrollRect.horizontalScrollbar != null )
        scrollbarHorizontal = new ScrollbarClass() { bar = scrollRect.horizontalScrollbar, active = true };

        if ( scrollbarVertical == null && scrollbarHorizontal == null )
            Debug.LogWarning ("Must have a horizontal or vertical scrollbar attached to the Scroll Rect for AutoHideUIScrollbar to work");
    }

    void Update ()
    {
        if ( scrollbarVertical != null )
            SetScrollBar( scrollbarVertical, true );
        if ( scrollbarHorizontal != null )
            SetScrollBar( scrollbarHorizontal, false );
    }
 
    void SetScrollBar ( ScrollbarClass scrollbar, bool vertical )
    {
        if ( scrollbar.active && scrollbar.bar.size > disableRange )
            SetBar( scrollbar, false, vertical );
        else if ( !scrollbar.active && scrollbar.bar.size < disableRange )
            SetBar( scrollbar, true, vertical );
    }

    void SetBar ( ScrollbarClass scrollbar, bool active, bool vertical )
    {
        scrollbar.bar.gameObject.SetActive( active );
        scrollbar.active = active;
        if ( alsoDisableScrolling )
        {
            if ( vertical )
                scrollRect.vertical = active;
            else
                scrollRect.horizontal = active;
        }
    }
}
5 Likes

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

1 Like

Thanks a lot for this! As @puzzlekings said, it should be part of the default implementation

Thanks!!! :slight_smile:

@Essential Thank You!

Not working at all.

It was 2014, maybe something has changed. Have you tried selecting the “Auto Hide” option in the scroll-rect?

7457867--915350--Immagine 2021-08-29 155205.png

The scrollbar and scrollview prefabs have a “normal color” property where you can assign its alpha value to 0, making it transparent.

8570033--1147271--upload_2022-11-7_22-7-56.png

2 Likes

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.

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Scrollbar))]
public class AutoHideScrollbar : MonoBehaviour
{
    private Scrollbar scrollbar;

    private void Start()
    {
        scrollbar = GetComponent<Scrollbar>();
    }

    private void Update()
    {
        scrollbar.targetGraphic.gameObject.SetActive(scrollbar.size < 1f);
    }
}