- Home /
4.6 UI - how to make a dynamic scrollbar?
How do you make in new 4.6 UI a scrollbar to appear only when the content size is greater than the ScrollView size?
I guess you have dynamic content in your scroll view. I've been trying to figure out how to add prefab panels to a scroll area for weeks.
The answer bit: Well I guess you could check the size of the parent and if the child is greater then enable the scrollbar.
The question bit: If you can give me an example of how to successfully add new GUI prefab panels with child objects to a scrollrect and not have weird aspect ratio issues or the panel not scrolling I'd be eternally grateful (realistically a few weeks of grateful!) :¬)
I know, I know I shouldn't ask a question in an answer, but my answer did have an answer. If you can answer my problem just let me know and I i'll ask the question so others can benefit form it.
I am just trying to find if there is a way to substitute OnGUI with the new 4.6 UI in dynamic menus, scrollareas and so on. Looks like the Legacy OnGUI is still indispensable when it comes to dynamic layouts and scrollareas.
No I don't think it is. OnGUI is so wasteful, lots of people have found solutions but it's early days for the new UI.
What's a shame is I had DFGUI and it worked similar to the new GUI but prefabs worked so much better.
When I upgraded to 4.6 the menus I had with DF were suddenly tiny so I took the route of going with the new UI.
This sounds interesting. I might tackle it in a YouTube video at some point. $$anonymous$$y general approach would be to use SetActive to enable and disable scroll bars. You should be able to compare the bounds of the RectTransforms directly to get the size.
For adding prefabs to an existing UI panel you can't go past the layout elements.
You might be right, @Bored$$anonymous$$ormon, but since in OnGUI.ScrollView this was just happening automatically, I expected the same to be happening in UI. Or just a checkbox for the scrollbar to dissapear when the content size is less than the ScrollView size. But looks like that requires some extra scripting.
Answer by Mmmpies · Dec 17, 2014 at 11:41 AM
Various way's to do this it's up to you but not difficult to script.
If you're scrollbar is likely to be dynamically then add a CanvasGroup and
if (myScrollbar.size == 1) { myScrollbar.GetComponent<CanvasGroup>().alpha = 0; myScrollbat.GetComponent<CanvasGroup>().interactable = false; myScrollbar.GetComponent<CanvasGroup>().blocksRaycast= false; }
Of course you can setActive as well or make that gameObject enabled = false so it's up to you for the approach. And you'll need an else to switch it on if not == 1.
Thanks. Seems like there is a lot to learn about the new UI system. The old OnGUI still seems more straightforward to me and easier to start.
Answer by Darklighte · Dec 23, 2014 at 07:22 PM
The previous answer has the correct approach as far as I can tell. The reason for checking the scroll bar height, instead of the content rect against your scrollview rect, like you would naturally think to do has to deal with the order of execution in the RebuildLayout method, unless you aren't using any autolayout componenents and are calculating it on your own.
I recommend however that instead of adding a canvas group component, unless you'd like to have a non interact-able but visible scrollbar, would just be to get the images of the scrollbar and handle and set their enable to false. This will accomplish the same thing without an extra component.
I used the canvasGroup because I had issues with the scrollBar not picking up new values if it's not enabled. $$anonymous$$ind you that was on one of the beta releases so might not be an issue now.
There are still issues with the == 1 approach, In testing an identical menu can have a size == 1 or .98 or even lower.
So I've setup a tab menu for spells, you can have all spells, contact spells, range or self. When I click on the tabstrip button the menu is dynamically built for the same list.
One of the tabs is very close to fitting on the screen with only a small overlap in the scrollPanel but it can build and == 1 then I click another tab and back on this one and it == .97, back off and back on and it == .98.
Early days for the new UI, I still like it but it needs work.
Your answer
Follow this Question
Related Questions
Scrollbar touch sensitivity 0 Answers
Why is my scrollbar not working? 4 Answers
How to disable ScrollView dragging 4 Answers
Smooth scroll snap? 3 Answers
Scroll Rect Min/Max Size 0 Answers