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 How to change size and position of scroll rect cells?

Discussion in 'Scripting' started by JimWebs, Apr 14, 2024.

  1. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    126
    As title says, I'm trying to change the size and position of the cells inside the scroll rect., I have it set up with content or container having a grid layout attached, constraint set to fixed column count. The first cell is an image with text child. I'm creating a copy of this in script and adding it to the scrollrect but when I try changing the size with

    Code (CSharp):
    1. newitem.GetComponent<RectTransform>().sizeDelta = new Vector2 (300,300);
    it still appears with the size set in the scrolling area.

    Likewise with position, it sticks to scrollrect setting.
    I have a content size fitter on the child of the item.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,067
    The GridLayoutGroup contains the controls for how big a cell is. Look for
    .cellSize
    I think it is.

    Also, try to avoid crazy psycho-ninja code like this:

    Make a public field, drag the thing you want in:

    Code (csharp):
    1. public GridLayoutGrid myGrid;
    Then manipulate it directly:

    Code (csharp):
    1. myGrid.cellSize = new Vector2 (100,50);
     
    Ryiah and CodeSmile like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,530
    If you aren‘t that far into making GUI: use UI Toolkit.
     
  4. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    126
    Thanks. I'm having a look now. ... and lol all of my code is psycho ninja. Probably a lot of stuff that would make a grown man cry :)
     
  5. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    126
    I'm not sure what ui toolkit is, but I think it's too late for me now.
     
  6. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    126
    Yep GridLayoutGroup changes the cellsize but for all of the cells. I'm trying to change the cells independently as I add them for different sized items but still have them in the grid and scrollable.
     
  7. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    126
    I tried adding a layout element (during gameview testing) to the cells that I'm adding but still position won't change.
     
  8. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    126
    Got it now! And it still scrolls with all the changed sizes and placements. Thanks for pointing me in the right direction.