Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
4
Question by gumboots · Aug 03, 2016 at 07:59 AM · uiscrollviewresolutions

Dynamic window size - Scroll View, Content Size Fitter and Screen Edges

Hi there!

I'm wondering if it's possible to use a Scroll View and Content Size Fitter, to have an expanding panel based on its content, but when the panel reaches the edges of the screen, the scroll bars kick in?

It's to handle showing content at different screen resolutions. I don't want to stretch the panel the screen's height the whole time, as it will look empty. I'd rather have it as small as possible, but if that is larger than the screen, simply add a scroll bar.

Any help is greatly appreciated!

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

5 Replies

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

Answer by gumboots · Aug 06, 2016 at 04:05 AM

I solved this using the post by @Brathnann here. Basically:

  • The ScrollView 'Viewport' should be set to the max size you want it to be.

  • The 'Content' is where the content goes. 'Content' should have a ContentSizeFitter and some type of LayoutGroup, e.g. VerticalLayoutGroup. This will allow the content box to expand, but the Viewport will act as a mask.

  • Scrollbars will hide automatically depending on the setting on the ScrollView.

Comment
Add comment · Show 5 · 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 Reyan62 · Feb 20, 2017 at 09:31 PM 1
Share

I was just looking for the answer to this question too. This does not answer it - it's a fine description of how to make a normal scroll window with a fixed size.

The question is asking how to make the viewport match the size of the content until a certain maximum size, at which point it is capped and becomes a scroll window. I have not found a solution to this problem yet, so my guess is that it might only be possible with scripting

avatar image gumboots Reyan62 · Feb 20, 2017 at 10:06 PM 0
Share

It's been awhile since I did this, but I'm fairly certain the answer I posted does solve what you're after. The 'Content' will continue to expand, up until it reaches the 'Viewport' size, where it will become a scroll window.

You obviously have to have the extra additional internal element that does the expanding, but unless you can't have that, this does solve the issue, doesn't it?

avatar image Reyan62 gumboots · Feb 20, 2017 at 11:15 PM 1
Share

Thanks for the response. $$anonymous$$aybe I'm not understanding, but I don't think it is a solution. What I want - and what I believe the OP is asking - is for the Viewport itself (in addition to the Content) to shrink to match the content size if the content is small enough - but never to expand past a given size (in OP's case, the maximum size being the screen size).

Show more comments
avatar image Chris_So · Mar 17, 2020 at 03:25 PM 1
Share

This is a lackluster solution to the problem. Yes, I get a scroll view, but it will always be the same size. Even if there is a lot of screen space left for it to cover. The solution @Daerst provided does what I want. The scroll view expands as long as there is space left. Once there is no space left, it starts to scroll.

avatar image
21

Answer by Daerst · Mar 17, 2020 at 03:17 PM

Here's a solution relying solely on Layouts and LayoutElements, no ContentSizeFitters.

By default, you have a hierarchy like this: Scroll View -> Viewport -> Content -> Elements Let's start from the bottom. Content gets a VerticalLayoutGroup, not controlling the height. You can now add elements to it as you like, their height will be unchanged.

In order to resize the Content to its children, we add a VerticalLayoutGroup to its parent (the Viewport object), and set it Control Child Height. This will make sure the Content object fits its children.

Now, we're in a pickle regarding the Viewport object. We want it to grow with its children, but not exceed the screen. The Content, based on its children, reports a MinHeight, and this is the first thing the layout system will try to realize. In this special case however, it's perfectly fine for the child (Content) to exceed the parent (Viewport), so add a LayoutElement to Viewport and overwrite the MinHeight with 0 (or any other minmum value you'd like to use).

In order to not break the scroll bars with this next step, add LayoutElements to both and set them to ignore layout. Now, to atually resize the Viewport object based on its Content child, add another VerticalLayoutGroup to the Scroll View and set it to Control Child Height.

Now add a screen- / window-sized RectTransform, give it a VerticalLayoutGroup that Controls Child Height, and throw in your ScrollView, and you should be fine.

Result

Comment
Add comment · Show 6 · 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 Daerst · Mar 17, 2020 at 03:24 PM 2
Share

For reference, here is the configuration in my test scene: https://imgur.com/J43x2f$$anonymous$$

avatar image bobbaluba Daerst · Jul 01, 2020 at 03:11 PM 1
Share

Thanks! This works almost exactly the way I needed. The only thing I couldn't figure out, was how to have elements in the content use layouts... I.e. paragraphs of text with dynamic content.

If I do that, then the size of the window end up using old height values, always lagging one layout recalculation behind the content.

I ended up writing an ugly script to set the rectTransform height of each text element to the Text$$anonymous$$eshProUgui.preferredHeight.

avatar image rtilton1 · Jul 23, 2020 at 11:10 PM 1
Share

Best answer for sure! Thanks for sharing.

avatar image ZOTGames · Oct 21, 2020 at 03:23 PM 1
Share

Best solution ever thanks for share this!!

avatar image aromana · Jul 24, 2021 at 12:14 AM 1
Share

I made an account just to say THANK YOU. If you ever find yourself in Oakland, CA, please hit me up because I owe you a beer.

avatar image diggyk aromana · Jul 24, 2021 at 12:46 AM 1
Share

Oh man, I just moved to Michigan from Oakland!

avatar image
0

Answer by Cunnah · Aug 03, 2016 at 01:27 PM

This is possible by working out how big the content will be and altering the scroll view and the scroll view content RectTransform. The scroll view allows for the ability to turn the scroll bar on or off is the content RectTransform is smaller than the scroll view window.

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 gumboots · Aug 06, 2016 at 04:01 AM 0
Share

Thanks for the reply! I used the reply from here, which I think is what you were alluding to?

avatar image
0

Answer by SeppF · Jun 26, 2017 at 09:56 PM

Maybe late... But I handlet it with a content size fitter on the viewport. Just check the viewport rect with the maxvalues u want and Disable its fitter if it reaches max.

Comment
Add comment · 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
0

Answer by diggyk · Jan 03, 2021 at 05:04 PM

Thanks for @Daerst for his insights above (his is truly the best answer). I want the panel to also be centered in the middle of the screen and expand vertically so I took what he wrote and figured out how to do it and have detailed it in my document titled Up-Down Expanding Panel with Center Expanding ScrollView


centerexpandingpanel.pdf (357.1 kB)
Comment
Add comment · 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

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

83 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

Related Questions

Help needed with scrollrect in specific UI design 1 Answer

How do I dynamically update a rect scroll from a List? 0 Answers

Changing ScrollView content from the script 0 Answers

Scroll Rect Min/Max Size 0 Answers

ScrollRect makes object behind it move 1 Answer


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