Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Betina_art · Nov 14, 2021 at 11:41 AM · unity 5uisliderscrollscrollbar

How to keep the scroll bar handle at a fixed size?

Hi!

I'm trying to do a scrolling list, but when designing how it would look, i want the handle to stay at a fixed size no matter the size of the list (so I can make a custom sprite for it). Basically like a slider than a scrollbar, but I can't pass in a slider to the scroll rect component.

Is there a way I can restrict the size on the scrollbar's handle? Or a way I can use a slider to move the list instead of a scrollbar? Or maybe make the handle sprite a child of the handle object but have it actually reach the ends of the bar when it moves?

alt text

untitled.png (33.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by asafsitner · Nov 14, 2021 at 12:51 PM

It is possible to use a Slider for scrolling a ScrollRect by using ScrollRect.verticalNormalizedPosition. You can assign the value of the slider to the verticalNormalizedPosition (if you need to normalize the Slider's value just divide the current value with the max).


Also, since for a ScrollRect a verticalNormalizedPosition = 0 is the bottom, you may want to flip the orientation (or value) of the slider.

Example script:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ScrollWithSliderTest : MonoBehaviour
 {
     public ScrollRect Rect;
     public Slider ScrollSlider;
 
     private void Update()
     {
         Rect.verticalNormalizedPosition = ScrollSlider.value;
     }
 }


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Betina_art · Nov 14, 2021 at 03:01 PM 0
Share

Thank you! I'll try this out!

Will this only affect the scroll rect when using the slider? How about if I scroll by other means (like using touch/gamepad navigation inside the scroll rect), will it also update the slider's position? If not, is there a way I can do that without them canceling each other out? Is there something I can check to know if the scroll rect moves/changes?

avatar image asafsitner Betina_art · Nov 14, 2021 at 09:22 PM 1
Share

Unfortunately, this will indeed prevent scrolling by any other means, as the Update function enforces the ScrollRect to the Slider's value. :(


To fix that, we'll want to use the Slider.onValueChanged event, like so:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ScrollWithSliderTest: MonoBehaviour
 {
     public ScrollRect Rect;
     public Slider ScrollSlider;
 
     private void OnEnable()
     {
         ScrollSlider.onValueChanged.AddListener(UpdateScrollPosition);
     }
 
     private void OnDisable()
     {
         // important! Don't forget to unsubscribe from events! 
         ScrollSlider.onValueChanged.RemoveListener(UpdateScrollPosition);
     }
 
     private void UpdateScrollPosition(float value)
     {
             // Here I flip the value in the code instead of trying to rotate the UI element itself since it's easier for me :P
         Rect.verticalNormalizedPosition = 1 - value;
     }
 }


That will allow us to maintain other methods with the ScrollRect, but now the problem of updating the Slider's value without causing an infinite loop remains. Unfortunately, Unity does not offer any solution to this out of the box, so a little more research into a workaround is needed.

EDIT:

It seems I was stuck in the past. Unity have since added the Slider.SetValueWithoutNotify API, which makes it very easy! Like before, we want to use the ScrollRect.onValueChanged event to update the Slider's value, but now we will call`SetValueWithoutNotify` to avoid the infinite loop:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ScrollWithSliderTest: MonoBehaviour
 {
     public ScrollRect Rect;
     public Slider ScrollSlider;
 
     private void OnEnable()
     {
         ScrollSlider.onValueChanged.AddListener(UpdateScrollPosition);
         Rect.onValueChanged.AddListener(UpdateSliderValue);
     }
 
     private void OnDisable()
     {
         // important! Don't forget to unsubscribe from events! 
         ScrollSlider.onValueChanged.RemoveListener(UpdateScrollPosition);
         Rect.onValueChanged.RemoveListener(UpdateSliderValue);
     }
 
     private void UpdateScrollPosition(float value)
     {
         // Here I flip the value in the code instead of trying to rotate the UI element itself since it's easier for me :P
         Rect.verticalNormalizedPosition = 1 - value;
     }
 
     private void UpdateSliderValue(Vector2 scrollPosition)
     {
         // Again, flippin the value for visual consistency
         ScrollSlider.SetValueWithoutNotify(1 - scrollPosition.y);
     }
 }


avatar image Betina_art asafsitner · Nov 15, 2021 at 02:15 AM 1
Share

It works perfectly! Thank you so much! You're the best!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

308 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create vertical scroll inside horizontal scroll? 1 Answer

How to put a image under a powerbar's handle thats moving? 0 Answers

Issue with implementing my own slider script. 1 Answer

Scroll Rect Min/Max Size 0 Answers

Unity 5 UI Scroll Rect Problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges