Search Unity

Handle size of a scrollbar

Discussion in 'Scripting' started by Rotzak, Sep 19, 2018.

  1. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    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?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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?
     
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    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.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Good point. I definitely jumped the gun there.
     
    Munchy2007 likes this.
  5. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    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*

     
  6. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    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.

    scrollbarfixedsize.PNG
     
    Neohun, sindhumca48, CoughE and 3 others like this.
  7. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    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.
     
  8. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    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.
     
  9. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    Finally I have found a solution. I've deleted the ScrollRect component and replaced it with this script:

    Code (CSharp):
    1. using UnityEngine.UI;
    2. public class ScrollRect_fix : ScrollRect {
    3.    
    4.      override protected void LateUpdate() {
    5.          base.LateUpdate();
    6.          if (this.verticalScrollbar) {
    7.              this.verticalScrollbar.size=0.5f;
    8.          }
    9.      }
    10.    
    11.      override public void Rebuild(CanvasUpdate executing) {
    12.          base.Rebuild(executing);
    13.          if (this.verticalScrollbar) {
    14.              this.verticalScrollbar.size=0.5f;
    15.          }
    16.      }
    17. }
    18.  
    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
     
  10. garamanx

    garamanx

    Joined:
    Apr 3, 2019
    Posts:
    11
    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 :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ForceOnValueChanged : MonoBehaviour
    7. {
    8.     public Scrollbar scrollBar;
    9.     public void Start()
    10.     {
    11.         scrollBar.onValueChanged.Invoke(0.999f);
    12.     }
    13. }
     
  11. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    71
    Does this problem still exist in version 2022? Is there a proper solution rather than a hack?
     
  12. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,147
    Last edited: Mar 22, 2024