How to make flexible menu

Good day. I am not new at Unity, and worked with UI a while, but still cant understand, how to make menu, when container UI proportionally control element inside it.
Here what I mean:

blue rect - is main container; red rect - is content element which contains Icon button + text;
Blue rect can be resized according to anchor points.
Red rect must fit to the Blue rect width, but also be flexible about it own height, because sometimes text field cant take 2 lines.

Blue rect got Vertical Layout Group with child expand
Red rect got Vertical Layout Group with child expand
Red rect icon got LayoutElement with preferred width and height (55)
Red rect text field got Content Size Fitter with height on preferred size

I make tests for a long time with it. Here is illustration

I totally can understand how to get that result.

Try playing with layout elements.
Example,

Add layout element to those red icons, set it minz as u desire,
Then on parent of those red icons, add vertical layout and deselect “Forced” height and width .
Set pivot of that object to Top middle
Add content size fitter in it
Set its Vertical fit to Min size.
Done

thanks for reply. I tried, but instead of min height I set preferred height. Result looks a bit prettier, but the main thing didn’t change - if to put many Red rect objects in Blue rect, they dont scale their height and width proportionally… It start to cover each other that way

Add spacing to them,check Vertical layout group properties

spacing will not scale them

I am working on exact same type of Scene today, and it is working for me properly…Make sure your anchors are proper…and only pivot of parent is top middle rest of pivot are center…If that did not work, post some screenshot of your settings etc to understand your porblem

looks like everything fine with my pivot…
Attaching my container prefab better

2161707–142897–container UI.unitypackage (6.54 KB)

I managed to have this result :

2161763--142899--result.png

With these components:

Nothing special on Item > Image (just a Layout Element with Preferred Width/Height to 55) and Item > Text.

Be careful with the anchors ! It is important.

Mask is not a solution for rescaling the objects.
Will explain in that way - If we put in container with height 100 some 4 objects with height 20, objects must keep their height. Now I got that result, that is fine, BUT
if we put in the same container 10 objects with height 20 (totally 200) they must be rescaled to height 10, and also proportionally rescale the width.

AH.

I have no idea how you can do that. And I’m not sure this is a good idea, you should put a limit to avoid this possibility.

I think you’ll have to create your own script which resizes height and width… !

Hi, I have downloaded your pacage and able to create this effect, let me know this is what you are looking for? so I can upload it back…

Imgur

not looks like… I think that screenshot will explain the idea
image 2163881--143082--exs.jpg

Actually I have tried to do it by scripts, but that is not so good way to do it, because the real size of UI elements (form RectTransform component) will be known only after Render order. So you can get some changes only to the next frame after some changes happens. There is a way to make coroutines, but it is so dirty…
And also I remember 1 project, where I had accordion menu (so height changes in every menu click) and that shall to fit the menu container height , not more or less. To make it need to make complicated system, to calculating the height, which will be on the next frame (count all spaces, items which will be shown etc)

I’m not worry about that.

The thing is, I think I know how to do that with a script, but the issue will be the text. At one point, the text container will be to small for the text himself (because of the size). So, if your item is too small, the text will disappear. However, you can avoid this by putting a min height. But… if you have too many items, it will be ugly.

I’m trying to do something.

Edit: ok, f*ck it ! :stuck_out_tongue:

You should try to make your own “Layout Element” and try to override CalculateLayoutInputHorizontal and CalculateLayoutInputVertical.

yea, you are right about the text… I didnt think about it. The only way - to change local scale of them… or may be in overwriting that Layout Element to scale content in it.
I will try it.
Actually there is one more simple thing, which I cant understand, why it is not working - standart button. I cant understand, why the parent container with component Button not fitting the size of the text inside it.
For example I need flexible width buttons, because Text inside can be different (use on text content size fitter). If I put content size fitter on the main object with Button component, that doesn’t fit that size…

@novaVision check this,

Imgur

1 Like

@novaVision : Actually, you can check “Best fit” ! The text size will depend on the container size.

2 Likes

sorry, have left for few days

nope, they are stretched. I still didn’t find a result of overriding layout component, to get result I need. I will share the result, when I get it.

not what I want. Sometimes need to fit container to text field, but not a text field to container. But in that case is possible to fit it manual by script on Text.text edit