Nested Content Size Fitter - How to go about simulating such structure

Hello,
I want to implement a scrollable comment list where it has a show more button. So this is my current structure:

  • ScrollView
  • Viewport
  • Content (contains ContentSizeFitter)
  • Comment prefab 1
  • Comment prefab 2
    …

So all of the comment prefab has, per say, a size of 200 when created. But if the text is longer than the text area and is clipped, when the user clicks the text box, I want the text box to expand and show all text.

Since I need to have a ContentSizeFitter in the content panel, I cannot seem to utilize ContentSizeFitter for the comment prefab, since they are nested. Am I doing something wrong?

If not, how would I go about calculating the correct size for the comment prefab when expanding? I tried using GUIStyle’s CalcHeight function, but depending on the length and format of the text, it seems to over or under-calculate the height. I cannot seem to find much about this, so would greatly appreciate any help!

Thank you!

So assuming the following. A comment is a long text string of someone’s comment. It will initially be in a fairly small box, so the text could be cut off. You want a scrollable list of these comments. If the user clicks on an individual comment it expands to the full size of the comment and collapses when clicked again. Multiple comments could be expanded.

Create the following in the Unity Editor:

  • UI Panel Object: Adjust its Rect Transform to Top Left: Name it Comment
  • Add a Layout Element Script as a Component of the Panel:Comment
  • Create a UI Text as a CHILD of Panel:Comment. Name it CommentText
  • Adjust the RectTransform of Text:CommentText to top Left
  • Add a Content Size Fitter to Text:CommentText. Disable it (uncheck the box in the top left). Then Set Vertical Fit: Preferred size
  • Adjust the Font, PointSize, RectTransforms Width/Height to get your desired Default Comment Collapsed Size.
  • In Panel:Comment Layout Component Check Min Height: Set it to Text:CommentText Height. (So our panel covers our initial Text)
  • Save this as a Prefab. Add Script I pasted below onto the Panel:Comment Prefab

Now For your main Object:

  • Create a ScrollView in the Project Scene:
  • In the Content Section add a Vertical Layout Group: Uncheck Child Force Expand Height (leave width checked)
  • Add a content Size Fitter and Set Vertical Size to Preferred Size.

Your Done. You can either drag the Prefabs as children of the Content. Or Instantiate them and set their parent to the Content.tranform. Don’t forget to add text to them if your testing. This will give you a scrollable list of Comment objects that will expand and collapse by clicking on them.

Script to attach to the Comment Panel Prefab

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class CommentController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

    private bool textExpanded;   // is our commment expanded or not
    private Text commentText;    // Object reference to the Text part of our Prefab
    private LayoutElement commentLayout;   // Object reference to the Layout component of the Panel
    private ContentSizeFitter commentTextSizeFitter;  //Object reference to the ContentSizeFitter in the Text Component
    private float initialHeight;  // Our Prefab Text's Original Height

    void Start() {
        // Initalize all our variables
        commentText = GetComponentInChildren<Text>();
        commentLayout = GetComponent<LayoutElement>();
        commentTextSizeFitter = GetComponentInChildren<ContentSizeFitter>();
        initialHeight = commentText.rectTransform.sizeDelta.y;
        textExpanded = false;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (textExpanded)
            CollapseText();
        else
            ExpandText();
    }

    public void OnPointerDown(PointerEventData eventData)
    { }

    private void CollapseText()
    {
        //turn off the size fitter and reset the Text to initialHeight
        commentTextSizeFitter.enabled = false;
        commentText.rectTransform.sizeDelta = new Vector2(commentText.rectTransform.sizeDelta.x, initialHeight);
        commentLayout.minHeight = initialHeight;
        textExpanded = false;
    }

    private void ExpandText()
    {
        // Turn on the size Fitter, and set the panel's Min height to the new size Fitter
        commentTextSizeFitter.enabled = true;

        // ContextSizeFitter is run in a Co-routine.  So we have to wait till its done resizing
        // We can't just wait until the commentText size is > InitialHeight because the comment might not have overflowed
        // So we set the size to 0, then wait for it to be bigger than 0
        commentText.rectTransform.sizeDelta = new Vector2(commentText.rectTransform.sizeDelta.x, 0);
        StartCoroutine(WaitForSizeChange());
    }

    IEnumerator WaitForSizeChange()
    {
        bool waitFlag = true;
        while(waitFlag)
        {
            if (commentText.rectTransform.sizeDelta.y > 0)
                waitFlag = false;
            yield return null; //wait for next Update
        }
        // ContentFitter got down resizing our Text.. now lets resize the panel
        commentLayout.minHeight = commentText.rectTransform.sizeDelta.y;
        textExpanded = true;
    }

}

Once you get that working correctly you could get fancy if time allowed and add a second UI Text to the Panel Prefab (call it Collapsed Text). When you pass in a comment to store in the comment Prefab, figure out how much fits in a collapsed text minus about 15 characters. Then append … show more to that text and when the Panel Prefab is in collapsed mode Turn that text on, and when its expanded turn off that text and turn on the Full text.

OOOHHH Thank you so much! Works like a charm! I’ve been struggling on this for hours.