0

I am building a UI-only application in Unity. I have a container with an Aspect Ratio Fitter of 1.77778 on it to ensure it always has a ratio of 16:9.

The problem is that when I adjust the width of the application, I need it to always make sure the container stays within the bounds of the parents, no matter if the user adjusts the height or width of the application. I've added every combination of Layout Element and Layout group to each container outside and inside and even played with the canvas scaler, but no matter what I do, I can only get it to work on height OR width adjustments, but NOT both. I need BOTH to be able to resize the container. Everything online tells me I can only do one or the other, but I doubt that a game engine like Unity has that weakness.

I can probably do it programmatically, but I feel like there's a way to do it otherwise.

I've included images to give examples of my basic layout.

enter image description here

enter image description here

KeepCool
  • 497
  • 1
  • 6
  • 24
  • Have you tried `Fit In Parent` -> `The width, height, position, and anchors are automatically adjusted to make the rect fit inside the rect of the parent while keeping the aspect ratio. The may be some space inside the parent rect which is not covered by this rect.` .. otherwise if you want to cover everything .. then just make your child stretched to the rect of the parent...? – derHugo Jan 25 '22 at 21:30
  • The problem is that the container already has the Aspect Ratio Fitter on it to keep the Ratio. And the "Fit in parent" option is in that. So I can't have both. – KeepCool Jan 25 '22 at 21:35
  • Actually, I'm dumb. You CAN put both (smh). So sorry. It seems to have worked. I had it set with two containers within each other due to something else I had in there before, so that was messing things up. Removed that second container and now it works! – KeepCool Jan 25 '22 at 21:41
  • Just confirming, are you using the **newer** _UI Toolkit_ (UI Builder, UXML) OR the **legacy** _canvas-style_ (Unity UI?) approach? –  Jan 25 '22 at 23:20
  • Oh I didn't realize there was a new way of doing UI. I'm currently using Canvas'. I'll have to take a look at the new one. – KeepCool Jan 26 '22 at 21:18
  • @derHugo You should post your comment as an answer, so I can give you credit at least. – KeepCool Jan 27 '22 at 17:19
  • @KeepCool the much I would welcome that ;) but `I had it set with two containers within each other due to something else I had in there before, so that was messing things up` -> I think this kind of makes your issue/question rather "Not reproducible or caused by a typo" and I would add "or a user mistake" ;) – derHugo Jan 27 '22 at 17:53

1 Answers1

0

Adding "Fit In Parent" in Aspect Ratio Fitter solved the issue. I just had an unnecessary extra container in there causing issues. Once I removed that, it worked like a charm.

As a side note, thanks to MickyD above, I've also discovered the new Unity UI Builder toolkit which is a game changer for UI building.

KeepCool
  • 497
  • 1
  • 6
  • 24