• 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
Question by rss181919 · Mar 12, 2017 at 07:31 PM · guitextguilayoutscroll viewcontentfit

how to create scrolling list of prefabs of different height

Unity 5 (New GUI)

I have a simple canvas > scrollrect > content container (vertical layout group with vert. fit = preferred size).

My content items are from prefab that is just a panel containing 2 text elements placed horizontally. Text element 1 holds a phrase number and text element 2 holds corresponding phrase. The phrases can be long and often cover multiple lines so the text 2 element is set for horizontal overflow = wrap.

I instantiate and attach my prefab objects in the SceneManager.OnSceneLoaded event of a game controller object script.

Everything seems to work fine except the sizing of the prefab height. I want the height to be calculated based on the text content (which will be anywhere from 1 to maybe 5 lines depending on the phrase length). So working properly I would get a vertical list of different height panels holding the phrase text. However, what I am getting is uniform height for all instantiated prefabs. I have tried about every combination of layout modification I can think of including adding "Content Size Fitters" at some or all the levels and also adding Layout Elements to see if overriding the auto layout on an element would fix it. I get some variation in the output but never close to what I want.

For the new GUI guru's out there, how should I go about accomplishing this. This seems like a common use case so I would assume the tool can handle it and I am just doing something wrong.

Comment

People who like this

0 Show 1
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 rss181919 · Mar 11, 2017 at 06:46 AM 0
Share

I threw a script on the Text object and added some code to see what the preferred height is at run time. I output that value to the screen using the update method. Turns out the preferred height is set correctly and varies by phrase length. So it seems something in one of the parent objects is overriding this and forcing all items to same height. As mentioned above my content container is a vertical layout group but it does NOT control or expand it's children. It's just there to align items vertically.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by rss181919 · Mar 16, 2017 at 09:12 AM

I found a few threads that led me to an answer.
My scrollrect was simply a panel with an image, a mask, and a scrollrect set to vertical scroll. My item container was a panel with vertical layout group with no child expansion or control. I also added a content size fitter with vertical constraint set to preferred height.
My item was a horizontal layout group with no child expansion or control. It also has a content size fitter with height constraint set to preferred height.
I had 2 text elements inside the horizontal layout group item panel. To each I added a content size fitter with height constraint set to preferred height.

All of this got me the correct height text boxes, horizontal text box container that matched the text height and an item container that matched the sum total height of its items. However, even though the item container was the correct height and the items were the correct height, the items did not reposition so that they all fit within the content container. The items overflowed the item container vertically. Turns out that after adding my prefab items in my script to the item container, I had to force a recalculation of the item container so it would reposition the items correctly. I used this code to force the recalc:

 `LayoutRebuilder.ForceRebuildLayoutImmediate (MyContentContainer.GetComponent<RectTransform> ());`
Comment
Wanderer13

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI layout ScrollView not scrolling 1 Answer

Inserting text into GUILayout Box? 3 Answers

Change gui box height based on text content? 1 Answer

ArgumentException: control 1s position in group with only 1 control 0 Answers

GUILayout.MinHeight and EditorGUILayout.BeginScrollView not respecting layout options. 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