Unity3d. UI elements snapping/anchoring - unity3d

I have canvas with vertical layout and 2 elements within (in fact it's element with only recttransform on it, let's call it container). So these 2 containers take a half of the screen by height and stretched by width, ok. How can I place an text element in above container and snap it to the bottom of this container? I tried press bottom button in recttransform widget (also with shift and alt) and it seems it doesn't affect my transform at all
P.s. May be I can use some free plugin instead of default unity components of UI layout?

There are different ways of placing your UI elements
Simply drag and drop it to the bottom where you want it
Use the anchor widget to set the anchoring to bottom with horizontal stretch and hold shift to also set pivot. Then set Pos Y to 0. Set Left and Right to 0.
Assuming you also want other elements in your containers, place a Vertical Layout Group on each container and make sure that your text element is the last child of the container in the hierarchy.
I would also advise you to seek out tutorials on Unity UI anchoring, positioning, scaling, and layout. You need a deeper understanding of how these things interact than you are likely to get from Stack Overflow. Otherwise you will suddenly find that your UI behaves in unexpected ways when rearranged or displayed on a different aspect ratio.

It's fairly easy with Unity UI system. You just need to get used to it. Here are simple steps to accomplish what you want:
Create Text element as a child of that container.
Select your newly created element and edit its RectTransform component values:
2.1. Set both Y axis anchors (min and max) to 0.
2.2. Set pivot value to 0 as well.
2.3. Set Pos Y value to 0 as well.
Now your Text element is anchored at the bottom of the container and its position (and height) is measured from the bottom of the Text element itself.

Related

Unity Vertical Layout Group height not calculating off children

I feel like I am not understanding the Vertical Layout Group component in Unity 2020.3.21 and I am hoping that someone can explain what I am missing.
I have a series of runtime-controlled UI components that I want displayed vertically top-to-bottom. I need to be able to toggle these components on and off at run-time, and have the parent Vertical Layout Group size automatically update to the height of all of the active children. This is exactly the sort of thing the Vertical Layout Group is supposed to handle, right?
When I build out a simple example, I am seeing that the Vertical Layout Group correctly positions the children in a vertical list. However, it competely ignores the sizes of the children when measuring it's own height. This is a problem because I want to be able to throw this container in a Scroll View -- if the height is not accurate, then the Scroll View doesn't work properly.
Here's my demo. I have a series of children in a VLG that all have explicit set heights in their RectTransform. All of them are direct children of the VLG. None of them use a LayoutElement; all heights are explicitly defined in a RectTransform. Here's the scene hierarchy, with the compile-time RectTransform heights of each component in parenthesis.
Canvas (325x812)
GameObject (812)
Vertical Layout Group (100)
Text (45)
Spacer (100)
Text Input (48)
Spacer (20)
Button (48)
Here are my VLG settings:
When I run the game, I can see the outline of each UI object in the Scene tab. The children clearly have their heights set. Yet the VLG still has it's static compile-time height of 100 instead of the sum of the child heights.
Am I missing something about how the Vertical Layout Group works? How can I get this VLG to have it's Height property set to the actual total height of it's active children at runtime?
ContentSizeFitter with Vertical Fit set to preferred size should do the trick. I did a quick test using simplified structure based on your example and got height calculated correctly (height of children objects is 100+50+75):

What's the proper way to align a strechable Text and an image to two ends in Unity UI?

I have an UI element that has a Text and an Image as its children. It has a HorizontalLayoutGroup component that enables Control Child Size and Child Force Expand.
I want the Image has a fixed size, and Text has strechable size controlled by the HorizontalLayoutGroup. So when the Image is set to inactive, the Text fills the whole space and when the Image is active, the Text shrinks a little bit in order to give space to the Image. Right now this part works good.
My second goal is to align them to both ends: the Text in the left and the Image on the right with space in between. But changing Child Alignment can't achieve this.
I tried the following solution:
Add LayoutElement both to Image and Text. On Text, enable Flexible Width and set a a value, on Image, enable Min Width and set to a value. Manually adjust the two values until it seems right.
This solution seems to work, but I don't know why. Is anyone familiar with this?What's the recommended way to do it? Thanks!
I worked it out. On the LayoutElement, treat Preferred Width or Preferred Height as Max Width or Max Height. Enable them, set the value the same with min value. One the other objects that you want to stretch, enable Flexible values. Then all worked as we want.

Unity list item is leaving extra space in high resolution

I have a vertical Scroll List. I have developed this app for resolution 768*1024. In this resolution my List is working fine. But when I run my app in higher resolution(1440*2960) it leave some space around all 4 direction.
I have also tried with changing Layout element min height dynamically, but Spacing issue is still exist.
Vertical and horizontal layout set element position in (screen width/height divided by a number of elements) * element number, in other words, they space out all elements evenly across canvas space. To achieve what you want you either have to enable child control size -> height option or write a script that aligns your elements in the center of the screen and one after another taking in consideration their height.

Can't get vertical layout group with scrolling working

I've got a panel which sits above my canvas and slides out upon clicking a button. I want to populate this panel with several objects (the number will be determined at runtime). Since the number of objects can exceed the number that would reasonably fit on a single row of the panel, I'd like to be able to scroll down with the mouse to new rows where the remaining objects have been populated.
To achieve this I've put a Scroll Rect and a Mask on the panel, created a child Image with a Vertical Layout Group to hold a series of child panels representing the rows that would hold the objects, and set the Image as the Content of the Scroll Rect.
The issue is that when I try to make one of the row panels a child of the Image object by dragging it to the Image in the Hierarchy, the anchors for the panel coalesce to a single point (the top left of the Image since it is set to Upper Left alignment) and when I try to manually drag the anchors to the appropriate position a box with a red X in it appears and expands in a manner that doesn't seem to correspond to the direction of the mouse.
Is this the right approach, and if so, how can I get this to work?
Thanks!

Evenly aligning things in IB

I have ten labels on a view positioned vertically. I need to evenly space them. Does IB have any type of setting that will do this?
If you select individual labels and drag them around the view, they should "snap" to certain guides around the interface. If you drag an element close to another element it should snap to about 8 pixels away, and that's the standard spacing between elements on the iPhone.
If you want more precise control, you can select an element and use the arrow keys to move it around one pixel at a time.
You can also use the Align Horizontally/Vertically in Container menu items from the Layout menu.
Do the math and then type in the X,Y coordinates. Unfortunately, I think that is the easiest way.
If you are OK using Apple's guideline spacing, dragging one label near another will generate a dashed line at a certain point. Do this for each label below the next, and they will be evenly spaced.