Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Elvthe · Dec 23, 2017 at 10:28 AM · uiresolutionlayoutaspect ratio

Preserving aspect ratio for layout group elements

I am trying to create level selection UI where each level is represented by a square button in scrollable horizontal layout group. Horizontal group height is relative to screen height and each button should be fill full layout group height.

My current solution seems to be working but Aspect Ratio Filter gives me a warning message. According to this thread, it shouldn't be done this way, but somehow it does exactly what it should be. However UI doesn't update until I hit the Play button. Currently it looks like this:

alt text

And it scales to 4:3 resolution like this:

alt text

  • Canvas is set to Scale With Screen Size

  • Scroll Rect is set to scroll Horizontal, Clamped, Viewport set to Mask

  • Mask convers the Scroll Rect

  • Horizontal Layout Group is set to Child Controls Size (Height) and Child Force Expand (Height) and has Content Size Fitter (Horizontal: Min Size, Vertical: Unconstrained)

  • Each Button has Layout Element (Min Width 100, Min Height 100, Flexible Height 1) and Aspect Ratio Fitter (Height Controls Width) - this element displays message that "A child of a layout group should not have a Aspect Ratio Fitter component".

How can I achieve the same result without errors or warnings?

button.png (80.7 kB)
4-3.png (13.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by DarkestDay · Sep 27, 2018 at 07:52 AM

I think having Aspect Ratio Fitter in a Layout group is a viable solution. The warning is there to make you aware of the fact that you could possibly make two components control same dimension, which will lead to unexpected behavior as the two of them will try to adjust the dimension concurrently. So you need to make sure you understand what you are doing to make it work. For example, if you need the horizontal layout group where the heights and alignment are controlled by the layout group, but the children of the layout group should keep the aspect ratio: 1. On the parent object with horizontal layout group check height in Child Controls Size and Child Force Expand, but make sure to uncheck the width in both. 2. On the child objects add aspect ratio fitter and set it to whatever ratio you like (it can be individual for each child) and set Aspect Ratio to 'Height Controls Width' You get a warning when you do this, but you have Horizontal Layout group controlling only height, and aspect ratio fitter controlling only width, so there's actually to conflict here.

This works for me in 2018.2.2f1

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VoidVenom · Mar 15, 2020 at 10:45 PM 0
Share

Thank you, this solved my issue! This solution still works in 2019.3.3f1.

avatar image
2

Answer by talrumers · Oct 31, 2018 at 02:25 AM

I'm just using the aspect ratio (height controls width) inside of an horizontal layout (contols only the height of children) and I have some wierd results when the canvas is not pixel perfect:

Pixel perfect: When the scene is loaded (with the UI elements mentioned) the UI looks good and acts as it should. alt text

Pixel perfect off: When the scene is loaded all the cards (with the aspect ratio components) are on top of eachother! alt text

Wierdly enough, when you disable and re-enable the horizontal layout group's object, it looks good again! Conclusion: not using pixel perfect on this design (aspect ratio inside horizontal layout) will mess up the UI, and only disable and re-enable the layout will fix it. Seems like a bug.


untitled22.png (65.1 kB)
untitled23.png (55.7 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gillemp · Jul 14, 2021 at 01:52 PM 0
Share

I ran into the same issue! I posted it here: https://forum.unity.com/threads/bug-aspect-ratio-filter-not-being-applied.1140694/

avatar image
0

Answer by gillemp · Jul 22, 2021 at 02:38 PM

An Issue has been created in the Unity Issue Tracker. You can vote for it here

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gillemp · Sep 01, 2021 at 10:43 AM 0
Share

As said in the resolution, the recommended workaround is using "LayoutRebuilder.MarkLayoutForRebuild(HorizontalLayoutGroup)". More info in the issue itself

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can a child element of Vertical Layout Group fit its Parent in Unity? 1 Answer

Need some help regarding UI layout in different Screen resolutions 1 Answer

Fixed aspect ratio/resolution independence 1 Answer

Maintain aspect ratio of UI element and all it's children and fit screen using black bars if necessary. 2 Answers

Use different sprites in new Unity UI for SD vs HD 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges