- Home /
How to lock element width or height in a vertical or horizontal layout?
I want to achieve the following for a group of buttons within a horizontal layout group but have been completely confused by the documentation that is provided:
I want to be able to lock one item, lock one element or limit the max width of an item whilst the others are flexible or free to resize their width. I believe that this would work the same with all of the layout scripts, Horizontal Layout, Vertical Layout and Grid Layout
Answer by benjaminvancaspel · Jan 16, 2018 at 05:14 AM
Rather unintuitively this is how it actually works.
You need to first make sure that your GameObject with the Horizontal Layout Group applied has Child Controls Size checked for "Width" and "Height". You then need to have Child Force Expand "Width" = unchecked, "Height" = checked.
Locked Item: Needs a Layout Element script added to it.
- Preferred width: set to the value you want.
- Flexible Width: 0 (if it's left at 1, it'll just be flexible with 100 added to it and yes you have to check a box for "flexible" when you want it to not be flexible)
Flexible Item: Needs a Layout Element script added to it.
- Flexible Width: 1
If you leave the Flexible Width off, it will just collapse the button's width.
The results are consistent once you get the hang of it, but the problem is bending your head to click on all the different settings with names that don't fit to get the right numbers.
Be careful if you're going to use this for a Vertical group to set Child Force Expand "Width=checked" and "Height=unchecked" on the layout group
Eternal life and forever free beer for you, my friend!
Why isn't this in the official tutorial? Great explanation, thank you!
When using Flexible Width: N you also have to set Preferred width: 0, otherwise it could be taken from the content (from image, for example).
Not only responding but taking the time to make this very telling illustration...it's fantastic, thank you!!
Your answer
Follow this Question
Related Questions
How can I make Horizontal Layout Group starts from right to left? 4 Answers
How to control "Horizontal" and "Vertical" axis using UI Buttons 0 Answers
Cannot set pivot for children of horizontal layout. 0 Answers
Simple way to fit rotated UI element to parent 2 Answers
Vertically Stationary Camera for 2D? 0 Answers