• 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
4
Question by SuperMasterBlasterLaser · Jun 19, 2015 at 08:16 AM · c#guiscalegrid

Grid Layout Group with scalable content

Hello.

I have a UI panel that has GridLayoutGroup component. I will add to this panel child UI panels by code. Sometimes there will be 5 children, sometimes 9 and there will be more. When I add these children inside GridLayoutGroup, there will be some empty spaces or children will be outside of my panel.

How to make added children scaled so they fill all space inside GridLayoutGroup ?

Comment
Add comment · Show 2
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 Baste · Jun 19, 2015 at 03:41 PM 2
Share

I guess you could replace the GridLayoutGroup with 9 HorizontalLayoutGroups nested inside a VerticalLayoutGroup, and set all of them to have child force expand on. That'll make the empty ones take up zero space, and the rest of them take up the remainder.

I'm pretty sure that the GridLayoutGroup is designed specifically to not resize it's cells based on the number that are filled.

avatar image SuperMasterBlasterLaser · Jun 19, 2015 at 05:36 PM 0
Share

O$$anonymous$$. I made as you suggested. Worked well.

5 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by taiku · Nov 12, 2015 at 01:52 AM

I found a solution to this but it involves creating a new GridLayoutGroup class so it is a bit hacky.

It lets the grid position the elements normally, then uses the width of the grid to calculate the size the cells need to be to span the entire row.

stretchy grid example

First grab the source of the original grid layout from Unity's bitbucket here . Create a new script, pasting that code in and changing the name (to something like StretchyGridLayoutGroup.

And finally replace line 201:

 SetChildAlongAxis(rectChildren[i], 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]);

With this code:

 float realsize = ((width - (spacing[0] * (actualCellCountX - 1))) / actualCellCountX);
 SetChildAlongAxis(rectChildren[i], 0, startOffset.x + (realsize + spacing[0]) * positionX, realsize);

And now the children will regrow to fill the entire area of the grid. They regrow based on how the grid positioned the cells, so if the grid decided 1 cell per row, it would take up the whole row, but if the grid decides 2 per row then the 2 cells grow to fill. So first the regular layout code does it's thing, then we grow our cells based on our width.

If this isn't exactly what you are looking for, hopefully it at least helps!


stretchygrid.png (87.7 kB)
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 Bezaleel1998 · Jan 31, 2018 at 06:29 AM 0
Share

are this can be fixed if just 1 item or Child ?

avatar image ookk47oo Bezaleel1998 · Jan 21, 2019 at 10:16 AM 0
Share

Hello,I figure it out.Use this code.

 float realsize = ((width - (spacing.x * (cellCountX - 1)) - padding.left - padding.right) / cellCountX);
avatar image
3

Answer by ankad · Aug 08, 2016 at 07:12 PM

  1. Add GridLayoutGroup as component of Panel.

  2. Add current script to the Panel as component.

  3. Attach your UI prefab (for example, InputField)

alt text

 public int rows;
 public int cols;
 public GameObject inputFieldPrefab;

 void Start() {
     RectTransform parentRect = gameObject.GetComponent<RectTransform>();
     GridLayoutGroup gridLayout = gameObject.GetComponent<GridLayoutGroup>();
     gridLayout.cellSize = new Vector2(parentRect.rect.width / cols, parentRect.rect.height / rows);

     for (int i = 0; i < rows; i++) {
         for (int j = 0; j < cols; j++) {
             GameObject inputField = Instantiate(inputFieldPrefab);
             inputField.transform.SetParent(gameObject.transform, false);
         }
     }
 }



gameunity-boxofideas-android-personal-opengl-41-20.png (13.2 kB)
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 WytchWare · Oct 26, 2017 at 04:05 AM 0
Share

Just wanted to say that I appreciate how much effort you put in to making this an excellent solution. I appreciate you!

avatar image Skulls23 · Jun 01 at 03:35 PM 0
Share

Still works 2020.3.19f1 Needs a bit of changes depending of what you need. But still, it's damn efficient

avatar image
2

Answer by TheGri108 · Dec 13, 2019 at 09:00 AM

For us, we had to check and make sure all the children of the grid, had their scale to one, because the scale gets changed for some reason. alt text Hopefully helps somebody!


untitled.png (3.1 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 St-Aaron · Mar 18, 2021 at 08:59 AM 0
Share

This has been an issue so many times for me that I set the localscale = Vector3.one; as standard practice every time I dynamically add to a grid.

avatar image
0

Answer by Hukha · Sep 15, 2020 at 12:49 PM

In case anyone needs this: This is solved in the UnityForums, 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 Tortuap · Jun 15, 2021 at 10:20 AM 0
Share

Your link gives & link-back the solution given in THIS thread.

avatar image
0

Answer by mfatihbarut · Mar 17, 2021 at 04:52 PM

The solution is this simple ,

    Vector2 YeniSize = new Vector2(549, 768);
         KartScreen.GetComponent<GridLayoutGroup>().cellSize = YeniSize;


thanks to this guy

https://www.youtube.com/watch?v=3NcH4DyqAKc

Comment
Add comment · 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

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

34 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

Related Questions

Multiple Cars not working 1 Answer

uGUI keep position and size of GUI elements when anchors change 3 Answers

How do I change the fount size of GUI 1 Answer

How To Scale GUI Based On Screen Resolution 1 Answer

Is there a way to use ScaleMode.Scaletofit on buttons? 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