Home > other >  How to restrict width AND height to fit inside parent?
How to restrict width AND height to fit inside parent?

Time:01-28

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

CodePudding user response:

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.

  •  Tags:  
  • Related