0

I'm attempting to create a scrollable UI object that has 2 sections with one on top of the other: a text section and a button list section. My main problem is that the text and list of buttons change in size depending on what content the player is viewing.

It seems that just throwing the text and buttons into an empty UI object doesn't work, because the empty game object does not take on the size of its children, and since the empty object does not have its own size, scrolling through its contents is not possible.

Is there maybe an element that I am not aware of that could help me out?

1 Answer 1

1

Here's the setup I use for Scroll. It seems you are missing the third object, the content object. It is what decides the physical size of your content size using a Content Size Fitter and a Layout Group. I decided to include the entire setup just in case you are missing something else.

Parent Object (Mask OR Rect Mask 2D) - Object that is the visual bounds of your UI
     -> Scroll Object (ScrollRect) - Actual scroll component that drives the UI scroll
          -> Content Object (Horizontal OR Vertical Layout Group AND Content Size Fitter) - Is the parent of your actual data in your scroll 

To explain this a bit better, the Mask is used when you have a nonrectangular asset that you still want to be your max viewing bounds. Anything outside of this Mask will not be rendered. A Mask is more expensive than a Rect Mask 2D, so if your viewport is a square or rectangle, then use the 2D component.

The Scroll Rect is the driver of the scrolling of UI elements in Unity. Make sure to check the box if it is horizontal or vertical. I assume in your use case you are using vertical. Make sure to set the Content field to be the child of this object and the Viewport to be the parent (the mask).

The Content Object has two components on it. The first being the Content Size Fitter which will force this object to be the size of its content. If your scroll is a vertical scroll, set the Vertical Fit to Preferred Size and leave the Horizontal Fit to be Unconstrained size. If it is the opposite, then set the opposite of what I just said. The other component on this object is a Horizontal Layout Group OR a Vertical Layout Group. The difference being which direction your scroll is. The checkboxes in the settings of this object matter quite a bit. Check out the docs on what each one does.

If you attach the components as I specified and set the proper settings on the layout group, you should be able to have as many dynamic objects with variable size swapping between text and buttons.

If you have any other questions let me know. UI can be a bit daunting at first but once you figure it out, it gets much easier.

Edit: To achieve an object that can change size with a text object and a button, you will need to use a combination of content size fitters and layout groups.

Here is an example hierarchy Hierarchy

Here is the resultResult

Using a combination of the layout groups and content fitters forces the container the text is in to be the size it needs to be. If you are making these assets static meaning none of it is data-driven at runtime, then this should work right away. If you are changing this data at runtime, you will need to use LayoutRebuilder on the parent objects to assure that the layouts rebuild properly. Let me know if you have questions.

5
  • My problem right now is that my content object is plain text with another section for buttons below the text. I already have a setup that can successfully scroll down dynamic text, but when it comes to appending buttons to the bottom of the text, I am clueless. Commented Apr 18, 2021 at 3:11
  • 1
    What do you mean by appending a button to text? You want a separate object that holds both your text and your button? You want it to just swap between text and a button in the scroll? I know how to make it so the gameobject will assume the size of both the text and the button if that's what you want.
    – TEEBQNE
    Commented Apr 18, 2021 at 4:58
  • 1
    I added an example with gifs.
    – TEEBQNE
    Commented Apr 18, 2021 at 6:35
  • That helps a ton, So pretty much everything needed a content size fitter + layout group lol. Thank you so much! Commented Apr 19, 2021 at 2:09
  • 1
    Well, it depends on how you want to approach your UI and what you expect to get out of it. But in your case and what I know about UI, yes.
    – TEEBQNE
    Commented Apr 19, 2021 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.