Layout issues in Unity - unity3d

I have a Vertical Layout Group with a Content Size Fitter, with children a TextMeshPro and a ScrollView.
(The TextMeshPro is not a problem, I only put it to show that I needed a Vertical Layout Group, so let's forget that)
Inside the ScrollView I have another Text element. This Text element expands both vertically and horizontally. I want the horizontality to be scrolled by the ScrollView and I want the ScrollView to adjust to the height.
So I want the ScrollView to be the same height as all of its internal elements. this is my goal. I don't want a scripted solution, I want to be able to do it directly from the hierarchy, where you define the layout of all elements.
However I am not able to do this, my idea was to put the Vertical Layout Group with a Content Size Fitter in the ScrollView, but if I do, I can no longer scroll the text horizontally, as it is constantly realigned based on the alignment defined in the Vertical Layout Group.
ps. At first you might think, but a text with a horizontal Content Size Fitter does not stretch vertically, what need is there to enlarge the ScrollView based on the height of the text? In reality I don't really use the Text component, I use a similar component created specifically for particular fonts, which also expand the text vertically, for this I need that the height of the ScrollView dynamically adapts to the child components, but I repeat, with a Vertical Layout Group, then I can't scroll anymore.
Solutions?

I had done well the layout system. Disable Pixel Perfect on Canvas fix the problem of the text realignment.

Related

How can I lay out the following screen in flutter

I am new to Flutter and trying to convert my iOS app to Flutter. However, I am having difficulty laying out the screen. Below is what the iOS screen looks like. I keep on getting red screens with layout errors saying it cannot layout the screen. When I add a column and add to that an image and a horizontally scrolling grid in Flutter I get the layout errors. How can I layout this screen?
I think it should have a column as the main widget. Then, a large image should be displayed, which displays centered width-wise on the screen. It should be constrained height-wise and wide enough not to distort the image. The photos come in a variety of heights and widths.
Then there should be a large text field centered width-wise in the central column. Then a breed text box is centered width-wise in the central column and followed by a line of stats—lastly, a city, state, and miles.
Then a centered list of icons with a single line of text-centered underneath each icon. Finally, the whole icon bar should be centered width-wise in the column.
Next, there should be a line of small photos of the cat, which is left aligned to the column and fills to the right going off-screen and scrollable horizontally. It should be a grid with a staggered layout width-wise. Each image should be a fixed height and a ratio of width to fixed height to fit the image without distorting it. The images should have rounded corners. The width and height of the source image are known ahead of time and need to be adjusted to fit the new fixed height.
Lastly, there should be a web browser that fills the width of the column with a height large enough to hold the content of the HTML without scrolling the web browser so the whole screen looks like it is one screen.
The whole screen should be able to scroll vertically but not horizontally to see the main column's total content.
Below is what the screen should look like:

The VLG component does not expand regardless of the length of the child

This is my hierarchy in the Unity scene:
Vertical Layout Group
ScrollView (Horizontal)
Text (Very long)
I set the graphics to work perfectly in portrait mode with mobile device screens. But when I use the graphics on a tablet, or when I rotate the device, I have to enlarge the elements, and here the problem arises.
I can't find the correct way to expand the elements, I want many ScrollViews in the Vertical Layout Group, and I want to be able to scroll horizontally to view the texts inside, that's what I'm asking.
The question is convoluted and complex to explain, I explain it in three different ways (talking about the same thing, so they may seem similar), you choose which one is more understandable to you:
Explanation 1
The Vertical Layout Group expands autonomously, covering a large part of the screen.
The problem arises when I have to decide how to enlarge the ScrollView, I cannot use the anchors because they are replaced by the Vertical Layout Group, and I cannot use the "Control Child Size" and "Child Force Expand" option, because it would expand beyond the Vertical Layout Group, if the text component inside was longer, thus making the Scroll useless.
I want the ScrollView to expand only within the limits of the Vertical Layout Group, and therefore not be affected by the length of the internal text component (so that I can scroll it) (PS. I cannot use a Layout Element, because the text can vary both horizontally and vertically, and because the ScrollView is equipped with another Vertical Layout Group (I need it to manage the vertical development of the text, if it want to expand in height))
Explanation 2
I would like the ScrollView to expand horizontally until it touches the limits of the Vertical Layout Group.
This I would be able to do by enabling "Control Child Size" and "Child Force Expand".
However, I would like one more thing, which is that the ScrollView does not expand beyond the limits of the Vertical Layout Group. This problem is caused when the Text component (of variable size) will be longer than the length of the Vertical Layout Group.
I would therefore like the ScrollView to expand into the Vertical Layout Group, without being influenced by the size of the child.
Explanation 3
Trying to seem clearer, to enlarge the ScrollView to the limits of the VLG, I have to enable both "Control Child Size" and "Child Force Expand" (I can't use anchors, because it's the same VLG that replaces them with its own), but putting "Control Child Size", if the child Text were to be longer than the VLG, it would make the ScrollView enlarge (and I don't want this to happen! otherwise the ScrollView, if it were as long as its component, would make no sense! I want that the ScrollView stretch to the limits of the VLG, but don't exceed them!)
Enabling "Control Child Size" and "Child Force Expand" in the VLG made the ScrollView dependent on the length of the child. However, by also enabling "Control Child Size" in an additional VLG in the ScrollView, the problem vanished. In practice, the ScrollView stops depending on the child, so it is it who controls, instead of being controlled.
After the clarification, I believe I understand what you'd like to achieve. If this is not what you'd like, please correct my implementation so I can answer the question properly.
Here is the hierarchy I have set up.
Here is the UI working with the current setup.
As the UI is rather extensive, if you make any changes at runtime, you will need to make calls to rebuild the layout if you happen to change the text's length. The scroll will not change properly in a single update cycle. To update it at runtime, you can make a call to LayoutRebuilder.ForceRebuildImmediate while passing in a proper RectTransform for rebuilding.
I may have misunderstood the question, but is this solved by just changing the anchor points of the ScrollView to fit the Vertical Layout Group (no "Control Child Size" and "Child Force Expand" needed):
Using Anchor points to stretch to fit the parent object:

Combine text and buttons in scroll rect

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?
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
Here is the result
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.

Horizontal scrollview with images constant height in Unity3D UI - How?

I want to implement scrollview in Unity3D 5 UI with horizontal images downloaded from web, they have different random aspect ratios, but I want them to be constant height. And width to be according their aspect ratio, like this:
I tried to use Horizontal Layout Group, but images get too little, so it can all fit in group, when I want them to be constant height and stretch parent's width. So, like, parent's widht is primary and it affects children, but I want children to affect total parent's width (expand it when adding new children images).
Any ideas how to achieve this? Thanks.
You need to use ScrollView with horizontal scrolling enabled and vertical scrolling disabled. Setup your hierarchy as in the image below:
On the GameObject named Content attach Horizontal Layout Group and Content Size Fitter with the following settings:
Then on all your images attach a Layout Element component and during runtime calculate the width using the aspect ratio of the image and height to the Content gameobject and assign this width to Preferred Width property of Layout Element Component: In the following image 300 is the calculated width for the image:
As a result any images you add as child to the Content gameobject will increase their parent's width and also you will be able to scroll horizontally to view all the image.
p.s. I wrote this answer in a hurry. Hopefully it will make sense to you if not let me know and I will improve my answer.

How to set which view expands in a UIStackView?

I was just experimenting with UIStackView. In this quiz app i have different sized imaged, so i want the image to fill the space between the top button and the question text at the top. The buttons have constraints on them so they should be any size under 50px. As of now the top button is the one getting auto increased size, but that seems kinda random.
According to what I understand you have all the items inside a vertical UIstack. The images have different sizes so the available space for the buttons will changes based on the size of the image. I suggest that you add constraints to all buttons to be equal in height. This way they will re-size based on the available space but they will all have the same height.