Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GridLayout inside scrollview - Not calculating correct dimenstions

Discussion in 'UGUI & TextMesh Pro' started by mog-mog-mog, Sep 13, 2014.

  1. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    Hierarchy order
    ScrollRect - Stretch full
    Content
    Grid - Stretch full . Layout= 100,100,0,0,1,1
    GridItems


    When I assign a scrollbar for the ScrollRect, it default set Size =1 and I cannot override it. Scrollbar assume that the height of Content inside ScrollRect is same as ScrollRect because rect transform is set to stretch. This is incorrect assumption for grid whose height/width are determined by grid items

    How to fix that?
     
  2. Osnauticus

    Osnauticus

    Joined:
    Sep 5, 2014
    Posts:
    6
    For the content Grid control you need to make it not stretch in whatever direction you want to scroll. So if you want it to scroll vertically make the content item with the GridLayoutGroup:

    Stretch horizontal, vertical unstretched, anchor anywhere

    Or opposite for horizontal scrolling. I'd suggest throwing a ContentSizeFitter on the object with the GridLayoutGroup as well.
     
  3. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    Thank you for the quick response. You are correct. If I do that, it'll work but I do not want to set fixed height.

    Consider I want to scroll vertically. If I make it vertically unstretched, then I need to specify fixed height - which is not what I want. Grid dynamically calculates height and width on runtime and I want content to use grid height on Runtime instead of static "0" height. I believe it is a problem with scrollbar as it need to read the content length.

    FWIW, Grid and scrolling are working fine, only scrollbar is not working - which is kinda redundant on mobile.
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,870
    I can't really tell from the info given what the problem is. If you think you've found a bug, please submit a bug report (with the project attached plus description of repro steps).

    If you don't think it's a bug but want help on the setup, I'd recommend recording a screencast (with Jing or similar) that shows the problem you're having. Make sure to include Scene View, Hierarchy, and Inspector in the view and show how the various GameObjects are setup, then how ti behaves at runtime and explain how it's different from what you'd expect or what you need.

    A screencast just says more than a thousand words, and at least a dozen images.
     
  5. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    You can repro the issue in Unity 4.6 test project scene -Scroll view.

    Video notes:
    1) Open Srcroll view scene in ui test project.
    2) Check scrollbar size, it is 0.18
    3) Check scrollview content, which is stretch in horizontal with width = 2911(fixed)>
    4) Now change scroll view content to stretch both.
    5) Scrollbar size is locked to 1.0. it locks the scrolling.
    6) User cannot scroll even on touch mobile.

    Note: when playing with rect transform. It calculates the position to 0.0000000001... and 0.9999999... Instead of using 0 and 1. It should round off float values to snap values.

    Here is the video link (Can't attach - 4MB limit) -
    https://docs.google.com/uc?export=download&id=0B5JrVYAqgjnHREVuWEluUnpkUjQ

    Defining fixed width does not make sense as width is dependent on number of items. Number of items are added dynamically on runtime.
     
  6. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,870
    That is by design. You're setting the content rectangle to stretch together with the view and always have the same size as the view. When the content rectangle is no larger than the view, there is nowhere to scroll to.

    The content area is solely determined by the content rectangle itself, and you're explicitly setting that to be the same size as the view. Whether some children of the content rectangle are outside of the rectangle doesn't matter, only the content rectangle itself determines the content size. This way you can have items that are only partially inside the content area, or which is animated to move from outside the content area and into it or vice versa.

    May I ask why you're setting the content to stretch in the first place? It doesn't sound like the behavior you actually want.
     
  7. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    Thank you for the quick response.

    Consider screen size of 100wx900h and item size of 100wx100h. I want to show maximum items on screen and allow user to scroll vertically for remaining actions. I do not need scrollbar and prefer mobile touch scrolling. Screen can show 9 items vertically safely without any scrolling.

    Number of elements in grid are dynamic in nature and can vary from 5-45(for instance). In order to accommodate 45 items, I can set grid height to 4500. The problem in setting height to fixed 4500 is -
    Even if I've only 5 items, it'll still show the content height as 4500 and show blank area in the bottom 4000 pixels. This is not the desired behaviour as I want grid to scroll till last grid item not beyond that. How would you solve this problem?

    Once again, thank you for the help !!!
     
  8. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,870
    Ah ok. You'll need to make the rectangle of the grid adjust to the size required for its content. You can do that by adding a ContentSizeFitter component to the same GameObject that the GridLayoutGroup is on. See the manual for more info.
     
    pea likes this.
  9. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    Wonderful, this solves the issue. Thank you very much.