Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Grid Layout with flexible column count and maximized item width (within constraint)

Discussion in 'UGUI & TextMesh Pro' started by MaskedMouse, Mar 16, 2022.

  1. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,093
    The grid layout group that Unity provides only provides it with a static cell size.
    The result is that whenever you have a larger or smaller screen than designed you'll get a large empty space because Unity keeps the cell size the same and cannot fit another item in. Thus putting it on the next row.
    I want to maximize the width of my items with Min/Max constraints but fit them along the full rect width.

    Which creates a problem of having a variable width of cells and a variable column count.
    So let's say the minimum cell size is (100x, 50y) and the maximum is (200x, 100x).
    Spacing between them is (8x, 8y).
    Padding is (Left 8, Right 8).
    The width of the rect is 1920.

    I need to find the optimal cell size along with the optimal column count to fit it along the width of the rect.
    I'm trying to maximize the cell size in favor of the columns. So less columns but larger cell size if within boundaries is preferred. But the cell size may not exceed its maximum.

    I'm having a hard time getting the math in there.