Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Mmmpies · Jan 18, 2015 at 02:47 PM · scale4.6scrollbaraspect ratio

4.6 UI ScrollBar force handle size to be fixed

Using the new UI with a ScrollBar is there a way to respect the aspect ratio / size of the handle?

If I use an image that I want to stay the same size, or at least scale with the UI on resolution change, but not shrink and grow to fill the slider area if the content of the scroll panel changes.

What I'm trying to do is use an image for the handle but that image doesn't scale well, quite happy for the button to have to travel the full length of the bar just to move the content slightly if that happens.

Even happy to have a wrapper script that uses a transparent ScrollBar and sends the value to it based on a value 0 to 1 depending on the size of a panel it sits on and the hadles position. Which is what I'm going to script if I can't find a better solution.

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

7 Replies

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

Answer by tkoknordic · Jan 28, 2015 at 05:20 PM

Hi,

I have a super small solution:

Put the Scroll bar object in ScrollRect OnValueChanged event and then select there Scrollbar.size and set the atribute to 0:

alt text

Then you still need to set the scrollbar.size to 0 at the start of your game. I did a small script for that:

 public class FakeScrollBar : MonoBehaviour {
     void Awake() {
         transform.GetComponent<Scrollbar>().size = 0;
     }
 }



screenshot-2015-01-28-184826.png (23.4 kB)
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 Mmmpies · Jan 28, 2015 at 06:01 PM 0
Share

That's a great solution but I cant see Scrollbar.size what version are you on or is it a Pro feature?

ScrollBar

scrollbar.png (30.5 kB)
avatar image tkoknordic · Jan 28, 2015 at 06:14 PM 1
Share

$$anonymous$$mmpies, It's not a function but a field and you have it there in your picture where you can see the "float size".

avatar image Mmmpies · Jan 28, 2015 at 06:34 PM 0
Share

Cheers @tkoknordic, that'll be my dyslexic eyes then :¬)

Good of you to take the time to post it.

avatar image
7

Answer by casimps1 · Jun 21, 2015 at 04:39 PM

The answer is that you shouldn't be using a scrollbar. As far as Unity is concerned, that scaling is by definition part of Scrollbar behavior.

What you want is to use their Slider widget instead.

http://docs.unity3d.com/Manual/script-Slider.html

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 Piflik · Sep 07, 2015 at 12:28 PM 0
Share

This doesn't work in conjunction with a ScrollRect, though.

avatar image Aseemy · Jun 02, 2016 at 09:27 AM 4
Share

This is the correct answer, at least for me. All you need to do is to add this script to your slider.

 public class ScrollPosition : $$anonymous$$onoBehaviour {
     public Slider slider;
     public ScrollRect scrollRect;
     // Use this for initialization
     
     public void ChangeScrollPos()
     {
         scrollRect.horizontalNormalizedPosition = slider.value;
     }
 }

And use ChangeScrollPos() function in your sliders OnValueChanged. This is exactly what i did in my game Colors

avatar image c8theino Aseemy · Feb 03 at 12:46 PM 0
Share

Your code does not support changing the slider position by moving the scroll rect directly by swiping. I modified your code a little to make it work both ways.

 public class VerticalSliderScrollPosition : MonoBehaviour {
     [SerializeField] private Slider slider;
     [SerializeField] private ScrollRect scrollRect;
 
     private void Awake() {
         slider.onValueChanged.AddListener(ChangeScrollPos);
         scrollRect.onValueChanged.AddListener(ChangeSliderPos);
     }
 
     public void ChangeScrollPos(float value) {
         scrollRect.verticalNormalizedPosition = value;
     }
 
     public void ChangeSliderPos(Vector2 vector) {
         slider.value = vector.y;
     }
 }

No need to use OnValueChanged from inspector with this code.

avatar image
6

Answer by immeasurability · Nov 17, 2016 at 11:39 AM

replace base class of sroll rect

 using UnityEngine.UI;
 
 public class ScrollRect_fix : ScrollRect {
     
     override protected void LateUpdate() {
 
         base.LateUpdate();
 
         if (this.horizontalScrollbar) {
 
             this.horizontalScrollbar.size=0;
         }
     }
     
     override public void Rebuild(CanvasUpdate executing) {
 
         base.Rebuild(executing);
 
         if (this.horizontalScrollbar) {
 
             this.horizontalScrollbar.size=0;
         }
     }
 }

Comment
Add comment · Show 4 · 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 Sarchophagi · Feb 20, 2018 at 10:02 PM 0
Share

@tkoknordic 's answer not really working decently. This, on the other hand, works like a charm!

avatar image minearjade · Mar 29, 2018 at 05:56 AM 1
Share

Would propose an edit here. Use this modified version, and you can bypass all scrollbar resizing altogether.

 using UnityEngine.UI;
 
 public class ScrollRect_fix : ScrollRect
 {
     private float verticalScrollbarHandlerSize;
     private float horizontalScrollbarHandlerSize;
 
     override protected void LateUpdate()
     {
         if (this.horizontalScrollbar)
             horizontalScrollbarHandlerSize = this.horizontalScrollbar.size;
         if (this.verticalScrollbar)
             verticalScrollbarHandlerSize = this.verticalScrollbar.size;
 
         base.LateUpdate();
 
         if (this.horizontalScrollbar)
             this.horizontalScrollbar.size = horizontalScrollbarHandlerSize;
         if (this.verticalScrollbar)
             this.verticalScrollbar.size = verticalScrollbarHandlerSize;
     }
 
     override public void Rebuild(CanvasUpdate executing)
     {
         if (this.horizontalScrollbar)
             horizontalScrollbarHandlerSize = this.horizontalScrollbar.size;
         if (this.verticalScrollbar)
             verticalScrollbarHandlerSize = this.verticalScrollbar.size;
 
         base.Rebuild(executing);
 
         if (this.horizontalScrollbar)
             this.horizontalScrollbar.size = horizontalScrollbarHandlerSize;
         if (this.verticalScrollbar)
             this.verticalScrollbar.size = verticalScrollbarHandlerSize;
     }
 }
avatar image Rotzak · Sep 22, 2018 at 08:49 AM 0
Share

@immeasurability None of the above solutions is working properly but yours does. Thank you - 2018.

avatar image MikoGot · Apr 12, 2019 at 01:37 PM 0
Share

@$$anonymous$$earjade I tried your edit and it works perfeclty! Almost... there is this thing that Value changes from 0 to 1 and from 1 to 0 when there is no content. I don't know what causing it, but in Console I have warning: "Send$$anonymous$$essage cannot be called during Awake, CheckConsistency, or OnValidate"

avatar image
2

Answer by zXcongducXz · Jul 31, 2015 at 08:44 AM

In Scroll Rect (Script) Component

Set Movement Type = Clamped

On Value Changed (Single) add new, Drag Scrollbar (Script), Set Scrollbar\float size, set value = 0

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 kubaw638 · Sep 04, 2017 at 03:22 PM 0
Share

$$anonymous$$ake sure you set it in ScrollRect not in ScrollBar, otherwise it would produce glitches.

avatar image exoter · Feb 13, 2018 at 12:12 PM 0
Share

This is the solution that worked out perfectly for me

avatar image julian_at_pf · Apr 13, 2018 at 12:58 PM 1
Share

The initial value is not quite right, the handle still expands. Then when you drag it, it snaps back to the fixed size.

avatar image
1

Answer by Mmmpies · Jan 18, 2015 at 03:55 PM

OK got it sorted but it's messy.

Left a real scrollbar with a CanvasGroup on it and set Alpha to 0, untick Interactable and Blocksraycast and tick Ignore Parent Group.

Set the Scroll Rect script to use this ScrollBar.

Then put a Slider on the scene and fill in with whatever images you want to use. This is visible but I put a CanvasGroup on it so I can vanish it if the ScrollBar.size < 0.99f.

Then added this script onto it:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class FakeScrollBar : MonoBehaviour {
 
     public Scrollbar RealScrollBar;
 
     public void PassThrough(float scrollValue)
     {
         //Debug.Log (scrollValue);
         RealScrollBar.value = scrollValue;
     }
 }

Dragged the script onto the Slider and drag the invisible ScrollBar onto the public ScrollBar slot in the script.

Click + in the Sliders OnValueChanged and drag the Slider with the script onto the new slot that appears. From the dropdown select FakeScrollBar -> PassThrough.

It works but still interested if anyone has a better idea.

EDIT

What a massive idiot, RTFM @Mmmpies RTFM!

OK delete the hidden ScrollBar and change the the script to this:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class FakeScrollBar : MonoBehaviour {
     
     public ScrollRect MyScrollRect;
 
     public void PassThrough(float scrollValue)
     {
         MyScrollRect.verticalNormalizedPosition = scrollValue;
     }
 }

if working in the horizontal change the verticalNormaizedPosition to horizontalNormalizedPosition and a Slider now works as a ScrollBar, just drag the ScrollRect onto the public slot in the script.

Ah the benefits of a nights sleep and a really nice cup of tea.

Comment
Add comment · Show 1 · 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 simbaDraco · May 20, 2016 at 12:24 PM 0
Share

Than your edit worked like a charm but where is that parameter of float scrollValue co$$anonymous$$g from

  • 1
  • 2
  • ›

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

40 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

Related Questions

NGUI sprite scale aspect ratio 0 Answers

Any way to have tap screen movement controls that ignore GUI Buttons for all aspect ratios? 0 Answers

Resolutions Affect How Game Looks... 0 Answers

Preserve aspect ratio of UI elements, not just image inside it. (4.6 UI system) 2 Answers

Scaling particles across different aspect ratios 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