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
7
Question by sharatachary · Dec 06, 2014 at 10:27 PM · uiscroll

How to set UI Rect Scroll for dynamically growing content?

Could anyone kindly guide, how to use UI Rect Scroll for a content that might grown dynamically by adding content after a web service call or with respect to some button response by the user in game interface.

Following the tutorial available on Unity website, it show how to scroll an image which is greater in size than the scroll rect, but how to get this setup working for stuffs that might be added to the panel later in game or runtime, which will increase in height or width but still the scroll will reflect to the old height or width, rather than the new dimensions.

Kindly have a look at following image.

alt text

Could anyone guide me on this front.

Humble Regards, Sharat Achary

ui scroll rect.png (82.0 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

12 Replies

· Add your reply
  • Sort: 
avatar image
18

Answer by masoudarvishian · Jun 18, 2017 at 01:08 AM

This link solved my problem, I'm sure it helps you too.

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 jamesbtate · Nov 03, 2017 at 01:14 AM 0
Share

Thanks this is the best solution because it does not require code to calculate the size.

avatar image cmz-neu4590 · Mar 12, 2018 at 07:17 PM 0
Share

THAN$$anonymous$$ YOU!!! i had no idea this simple component “Content Size Fitter” existed, that solved my problem

avatar image Aracon · Oct 29, 2018 at 05:36 PM 0
Share

Helped, thanks!

avatar image karsnen · Mar 02, 2019 at 04:02 AM 0
Share

This is a much better answer & a solid one. A breeze.

avatar image Jon_Brant · May 05, 2020 at 04:21 AM 0
Share

Still working in 2019.3, thanks!

avatar image
10

Answer by KuR5 · Jun 06, 2016 at 07:26 AM

Find the attached demo. You can add objects under "Grid-Dynamic" gameobject dynamically and scrolling works fine.


dynamic-scroll-viewunitypackage.zip (8.1 kB)
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 SniperEvan · Jul 21, 2016 at 09:24 PM 0
Share

Thanks for this. :D :D

avatar image SniperEvan · Jul 21, 2016 at 09:32 PM 0
Share

I sent you some rep. This comment seriously made my day. I've been fighting with the ScrollRect forever and never had a good solution.

avatar image KuR5 SniperEvan · Jul 23, 2016 at 08:57 AM 0
Share

Thanks @SniperEvan, It is great that demo is being helpful to you.

avatar image abhilash-im · Nov 15, 2016 at 06:09 AM 0
Share

Excellent demo. Thanks for sharing the code.

avatar image julien-b · Jan 05, 2017 at 11:21 AM 0
Share

Hi, I need this too but I can't get the attached project to open...

avatar image unity_iLxAeHlWxpIIXw · Mar 29, 2019 at 06:44 AM 0
Share

i have tried several time but no zip file download can you send it to me?

avatar image
3

Answer by Mmmpies · Dec 07, 2014 at 10:00 AM

Think you need to add Component Content SizeFilter and set it to unrestrained

EDIT:

Well that's what I get for not checking my own menu item. BoredMormon is right you have to set the RectTransform to dynamically increase in size with enough for each item that you include.

In my case that's just vertical but it could be both.

See RectTransform.OffSetMax and RectTransform.OffSetMin

get the count of how many items are being loaded and multiply that by the how many times you're going to increase the x and y.

Not written the code to fix mine yet but if you're struggling I'll post the solution when I have it.

Comment
Add comment · Show 2 · 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 Kiwasi · Dec 07, 2014 at 10:27 AM 0
Share

You actually want to constrain it on the vertical. Also add layout elements to each button. Check out my UI tutorials for a video. It's not exactly what you are after, but you should be able to figure out all the pieces.

avatar image Mmmpies · Dec 16, 2014 at 10:45 PM 0
Share

Check out this video as well:

Unity 4.6 UI $$anonymous$$enu

I added the demo unity package to it under CursedIsle username to show how to build menu items from a list. As I say on the YouTube comments virus check and open in a new project so I don't break anything you've done. It was virus free when it left me but I can't be 100% sure of that once it's on the web.

Oh and follow both Stuart Spence and Richard Gubb on Youtube both well worth following.

avatar image
3

Answer by hghammoud · Jan 22, 2017 at 12:21 AM

I spent a lot of time to solve this issue. I tried every proposed solution on the web. None of them gave me the result i needed. I ended up calculating the height via a Script (MonoBehavior).

My aim was to calculate a 2D RectTransform (ScrollContent) that has children populated dynamically using the start() so the script executes once on update() after all has been initialized.

I added this to all the component with dynamic height and the scroll content.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ScrollExpander : MonoBehaviour {
 
     private bool _updated = false;
     // Use this for initialization
     void Update () {
 
         if (!_updated) {
             RectTransform rectTransform = (RectTransform)transform;
             float yMin = 0.0f;
             float yMax = 0.0f;
             foreach (RectTransform child in transform) {
                 yMin = Mathf.Min (yMin, child.offsetMin.y);
                 yMax = Mathf.Max (yMax, child.offsetMax.y);
             }
 
             float finalSize = yMax - yMin;
             rectTransform.sizeDelta = new Vector2 (rectTransform.sizeDelta.x, finalSize);
             _updated = true;
         }
     }
 
 }

Hope this helps some with this problem.

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 tarnumius · Dec 11, 2018 at 02:04 PM 0
Share

Thank you. You solution is best!

avatar image
3

Answer by nishant619 · Jul 11, 2017 at 10:36 AM

alt text

  1. Create a scroll view (right click > UI > Scroll View)

  2. Select Content, add Content Size Fitter component.

  3. Change the settings of both Content Size Fitter and Rect Transform like in the image below.

  4. Through your own script add text to Text component (dynamically).

     Text myText = this.transform.GetComponent<Text>();
     myText.text += "your desired text";
    
    
    
    
    

capture.png (57.0 kB)
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 Munchy2007 · Jul 11, 2017 at 12:32 PM 0
Share

You're example won't work properly without the addition of a VerticalLayoutGroup to the Content object.

You would also need to select Control Child Size Height on the VerticalLayoutGroup to make the Text component and Content area automatically resize to fit the text.

avatar image nishant619 Munchy2007 · Jul 11, 2017 at 03:29 PM 0
Share

Content Size Fitter is doing what u are asking. It's taking the preferred size from Text component and increasing size every time there is a new line in Text.text. $$anonymous$$y example is tested and works great.

avatar image Munchy2007 nishant619 · Jul 11, 2017 at 03:50 PM 1
Share

Oh I see what you've done, you've added a text component directly to the content gameobject, in which case it will work as you stated.

I prefer adding text objects as children of the content gameobject, as this gives the flexibility of being able to add multiple text objects or even other UI elements. In which case the verticallayoutgroup is required as I described.

Show more comments
avatar image aggamer · Oct 11, 2019 at 06:18 AM 0
Share

This worked perfectly as is in your example, thanks so much for posting this!

avatar image SimRuJ · Dec 09, 2021 at 12:25 PM 0
Share

Even in Unity 2021.1.24f1 you still have to add the Content Size Fitter manually to the Content (weird that it doesn't do it by default) but then it changes size accordingly. If you want to add multiple elements, I'd also recommend adding a Vertical or Horizontal Layout Group to align them properly (default: Control Child Size/Child Force Expand: Width). Btw, if the Scroll View itself is part of a Layout Group, then you can add a "Layout Element" component to it and it won't interfere.

  • 1
  • 2
  • 3
  • ›

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

26 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

Related Questions

Velocity of scrollRect always returns (0.0,0.0) 1 Answer

Clamping ScrollRect to Dynamically Created Content 1 Answer

How can I set the mousewheel scroll speed in a ScrollView? 3 Answers

Making the camera zoom in and out? 1 Answer

Scrolling Text 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