Search Unity

ScrollRect + Content size fitter not working in 5.2

Discussion in 'UGUI & TextMesh Pro' started by cesarpo, Sep 16, 2015.

  1. cesarpo

    cesarpo

    Joined:
    Jun 8, 2013
    Posts:
    97
    HI All, maybe someone can help me with this.

    I have a dynamic list that I want to scroll. My hierarchy looks like this:

    Code (CSharp):
    1. gameObject (main) : ScrollRect (content set to child content gameObject)
    2.   - gameObject (content) : Horizontal Layout Group (child force expand width) + Content Size Fitter (Horizontal fit min size)
    3.     - gameObject (item 1) : Layout Element (min width set to 100)
    4.     - gameObject (item 2) : Layout Element (min width set to 100)
    5.     - gameObject (item n) : Layout Element (min width set to 100)

    But I'm getting a warning in the Content Size fitter that prevents this setup to work, while it was previously working in 5.1:

    upload_2015-9-16_1-39-2.png

    What am I doing wrong?
     
    juanitogan likes this.
  2. terencetan

    terencetan

    Joined:
    Oct 29, 2012
    Posts:
    4
    I have the exact same problem. Is this a bug or is there a workaround?
    My setup is:
    ScrollRect : Content set to child
    - Grid Layout : Cell size 240 x 60. Fixed constraints(Content fitter component is here)
    - Element 1 (Dynamically added)
    - Element 2

    BTW there is no layout group component on the parent.
     
  3. cesarpo

    cesarpo

    Joined:
    Jun 8, 2013
    Posts:
    97
    Yeah it's weird, it was working in 5.1.

    Anyway, I fixed it by adding a empty gameObject called "viewport" as a parent of "content" and as child of the ScrollRect. The Content Size Fitter does not show the warning and everything else works perfectly.

    Just follow the manual: http://docs.unity3d.com/Manual/script-ScrollRect.html
     
  4. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    244
    I had the exact same set up and issue as cesarpo. His fixed worked for me. But it's got to be a bug.
     
  5. Chiefmofo123

    Chiefmofo123

    Joined:
    Jun 13, 2015
    Posts:
    2
    Yeah, I read in the release notes for 5.2 that this is a workaround for projects from 5.1 and the default setup for 5.2 onward. My problem is that when I place an intermediate node between the scroll Rect and the object with grid layout+content fitter, the intermediate node's rect transform doesn't update when the content fitter updates. This was possible in 5.1, but with an extra node, there is no way to update the scrolling area without scripting. Unless I'm missing something here...
     
    juanitogan likes this.
  6. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    Can someone fully explain this workaround? Unity Scroll rects are over-complex already without this extra workaround being thrown into the mix.

    Specifically: this new viewport object needs the Content Size Fitter in order to be resized, correct? But I'm finding that the Content Size Fitter on this new object is producing exactly the same 'Parent has a type of layout group component' warning and is not working at all.
     
    Last edited: Oct 4, 2015
    sasanostudio likes this.
  7. cesarpo

    cesarpo

    Joined:
    Jun 8, 2013
    Posts:
    97
    Us the same structure as the manual, and no, you have to add the Content size fitter to the Content gameObject.
     
    sasanostudio likes this.
  8. Tristannn

    Tristannn

    Joined:
    Aug 2, 2016
    Posts:
    3
    I found myself in the same place today. I solved it by creating a custom LayoutElement for the Scroll View.
    I didn't bother to build the component out into anything plug-and-play, but here's what I did:
    Code (CSharp):
    1.  
    2. public class LayoutScrollViewElement : LayoutElement
    3. {
    4.     public RectTransform contentRect;
    5.     public float maxHeight;
    6.  
    7.     override public float preferredHeight { get { return contentRect.sizeDelta.y > maxHeight ? maxHeight : contentRect.sizeDelta.y; } }
    8. }
    9.  
    all it does is pull it's preferred height directly from the content object up until a maxHeight. I also duplicated the LayoutElementEditor script, commented out the parts about preferredHeight and added my new properties.

    all in all, it's a bit of a hack, but for my own purposes I'm not concerned.

    Hopefully this helps someone!
     
  9. TharosTheDragon

    TharosTheDragon

    Joined:
    Sep 2, 2017
    Posts:
    13
    In the latest versions of Unity, all scroll views already have a viewport built in. The content size fitter still shows the warning.
     
    unnanego likes this.