Handle size of a scrollbar

Hello,

I have a scrollbar which is working great but on play the size of the scrollbar changes to 1. The size should be 0.5f. I've been looking for two hours now and I found alot of threads about the issue but no good answer yet. Can anyone help me?

The scrollbar resizes based on the amount of content in the ScrollRect, which is pretty standard UI behaviour across most operating systems. Could you elaborate on your goals exactly? Why would you want it to misrepresent the amount of content?

[quote=“GroZZleR”, post:2, topic: 715366]
The scrollbar resizes based on the amount of content in the ScrollRect, which is pretty standard UI behaviour across most operating systems. Could you elaborate on your goals exactly? Why would you want it to misrepresent the amount of content?
[/quote]

I’m not sure that’s actually true for a standalone Scrollbar component, OP doesn’t mention ScrollRect. Certainly I don’t see any automatic resizing of the handle of the Scrollbars I’m using in Unity 2018.2.

I can’t give any suggestions to help with the OPs problem unfortunately as I can’t reproduce it.

Good point. I definitely jumped the gun there.

1 Like

[quote=“GroZZleR”, post:2, topic: 715366]
The scrollbar resizes based on the amount of content in the ScrollRect, which is pretty standard UI behaviour across most operating systems. Could you elaborate on your goals exactly? Why would you want it to misrepresent the amount of content?
[/quote]
[quote=“Munchy2007”, post:3, topic: 715366]
I’m not sure that’s actually true for a standalone Scrollbar component, OP doesn’t mention ScrollRect. Certainly I don’t see any automatic resizing of the handle of the Scrollbars I’m using in Unity 2018.2.

I can’t give any suggestions to help with the OPs problem unfortunately as I can’t reproduce it.
[/quote]

Here is a video of the issue. As you can see the content that I want to display is very little. The handle of the slider is ridiculous small. Can’t edit the size because it’s set to zero on play. I said one in my first post but I meant it’s set to zero*

HideousBitesizedJaguar

For anyone still having this issue, as this is a common thing that I see on the forum a lot. There is a simple hacky thing I use to fix the size of scroll bar handles. It looks terrible but it does the trick. Simply add the Scrollbar.size call on the OnValueChange for the scrollRect with the value you want.
That's pretty much the same performance cost as setting a fixed size by code, without having to write any. But since you don't have thousands of scrollbars in the game it won't make any difference.

3699481--305164--scrollbarfixedsize.PNG

6 Likes

That's exactly what I found before. But it's still not perfect. When u load the scene where the scrollbar is in the scrollbar still has value 0. After you change the value of the scrollbar by scrolling, it changes to the size you want. I tried before to set te size in void Awake but couldn't figure it out.

Oh ok, that's going to happen because you never trigger the OnValueChanged, if you trigger it will fix. You can do that in several ways, but i guess the easiest way would be to add any object as child to the scrollRect content object;
If you set up your content to be autosize with the component "Content Size Fitter" it will force the layout to recalculate the content object size, triggering the OnValueChanged.

Also having the start value you want for the handle size on the scrollBar script could help.

Finally I have found a solution. I've deleted the ScrollRect component and replaced it with this script:

using UnityEngine.UI;
public class ScrollRect_fix : ScrollRect {

     override protected void LateUpdate() {
         base.LateUpdate();
         if (this.verticalScrollbar) {
             this.verticalScrollbar.size=0.5f;
         }
     }

     override public void Rebuild(CanvasUpdate executing) {
         base.Rebuild(executing);
         if (this.verticalScrollbar) {
             this.verticalScrollbar.size=0.5f;
         }
     }
}

This was an answer to https://answers.unity.com/questions/879346/46-ui-scrollbar-force-handle-size-to-be-fixed.html but didn't look at it first because of the votes

6 Likes

True that. I liked FernandoHC's solution, and in conjunction with it, I wrote this small script which scales the handle on start and thus gets rid of Rotzak's issue. Value 1 is not accepted somehow, but 0 and 0.999f work fine :)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ForceOnValueChanged : MonoBehaviour
{
    public Scrollbar scrollBar;
    public void Start()
    {
        scrollBar.onValueChanged.Invoke(0.999f);
    }
}
4 Likes

Does this problem still exist in version 2022? Is there a proper solution rather than a hack?

[quote=“jakesee”, post:11, topic: 715366]
Does this problem still exist in version 2022? Is there a proper solution rather than a hack?
[/quote]
Yes. Create a copy of the package for the project and modify the UpdateVisuals method at lines 299 and 300.

https://support.unity.com/hc/en-us/articles/9113460764052-How-can-I-modify-built-in-packages

Similar solution is to modify UpdateScrollbars() in ScrollRect.cs. Scrollbar's size field is set from there and the rest works with that size in mind.