Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by danmw3 · Sep 02, 2018 at 06:37 AM · uigridscalinglayoutconstraints

Scaling GridLayoutGroup cells for multiple mobile aspect ratios

I have a Grid Layout Group as the content of a Scroll Rect. The grid is constrained to 3 columns. What I would like to do is have the grid cells scale their size uniformly across multiple aspect ratios. Basically, I just want to maintain the same spacing between each of the cells and the edges of the screen by adjusting their sizes.

Below is what it currently looks like when running the game in 9:16 and 10:16 aspect ratios. The 10:16 ratio has larger gaps on the sides.

I am not sure how to proceed so all help is appreciated.

comparison

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

1 Reply

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

Answer by eses · Sep 02, 2018 at 10:08 AM

Hi @danmw3
There are many threads, answers and links to other sites about this topic if you just google for this.

It might be, that GridLayoutGroup and LayoutElements don't work that great together...IIRC.

If that was so, you can at least use built-in OnRectTransformDimensionsChange method, which gets called when RectTransform change.

Calculate your element size from the column count, RT width, spacing and padding, then apply it to gridLayoutGroup cell size.

But it probably isn't that simple, if you now 3x? amount of grid items, what about landscape layout... and so on. Might not be issue if you keep it simple and stick with vertical only.



Something like this (spacing is not good but works for this example):

     void OnRectTransformDimensionsChange ()
     {
         var w = rt.rect.width;
         var spacing = gridLayoutGroup.spacing.x;
       
         w -= spacing * 2f;
         size = (w / cellsX);
         
         // Optional size limits 
         // size = size < 32f ? 32f : size;
         gridLayoutGroup.cellSize = new Vector2 (size, size);
     }
Comment
Add comment · Show 2 · 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 danmw3 · Sep 02, 2018 at 11:22 AM 0
Share

I have been searching for well over two hours of issues similar to this. I have found a few, but none of them either had a solution for their specific situation or a solution in general that I could piece together.

I am not using LayoutElements for the grid, but I have seen other people try using them with little success.

This project only runs in portrait mode so that isn't a problem.

Either way, I have managed to figure this out without using the OnRectTransformDimensionsChange method because I only need to reposition everything on start.

Thanks for the help anyways.

avatar image eses danmw3 · Sep 02, 2018 at 12:22 PM 0
Share

@danmw3 - Ah. Ok. Anyway, I updated the answer with a code sample. It will give an idea what I meant, but it's not fully working for sure.

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

151 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 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

GridLayout Scaling Issues 1 Answer

How to Stretch objects in grid layout when screen size is changed 0 Answers

Scale Layout elements to fit screen 2 Answers

Why doesn't childing a gameobject to GridLayoutGroup at runtime align it properly? 1 Answer

Dynamically added buttons aren't aligned in GridLayoutGroup 0 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