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

How add texts to scrollrect at runtime

Discussion in 'UGUI & TextMesh Pro' started by oleggelezcov, Feb 9, 2015.

  1. oleggelezcov

    oleggelezcov

    Joined:
    Jan 18, 2013
    Posts:
    13
    I'm create panel for scrolling text objects. Texts instantiate runtime from prefab. I have hierarchy


    Content object has attached component VerticalLayoutGroup. Code for adding texts
    Code (CSharp):
    1.             for (int i = 0; i < 10; i++)
    2.             {
    3.                 GameObject inst = (GameObject)Instantiate(textPrefab);
    4.                 inst.GetComponent<Text>().text = contentString;
    5.  
    6.                 inst.transform.SetParent(layoutGroup.transform, false);
    7.             }
    Result view:

    Texts don't wrap horizontally and VerticalLayoutGroup rect don't fit to texts sizes.
    But i need get view something like this


    Describe steps for creating dinamically added texts to scroll, please
     
  2. Simie

    Simie

    Joined:
    Oct 26, 2012
    Posts:
    454
    Some things I'd check:

    - Make sure the text prefab has the Horizontal Overflow field set to "Wrap"
    - Are you using a ContentSizeFitter on the VerticalLayoutGroup? Make sure that is set to "Unconstrained" on the horizontal axis so it doesn't resize to the preferred size of the text. (Also make sure the VerticalLayoutGroup RectTransform is constrained to stretch to fit the parent horizontally.)
     
  3. oleggelezcov

    oleggelezcov

    Joined:
    Jan 18, 2013
    Posts:
    13
    Thanks, on Text Prefab in component LayoutElement set check on 'Min Width' and enter some value, on VerticalLayoutGroup->ContentSizeFitter set 'Horizontal Fit' = 'Unconstrained', 'Vertical Fit' = 'Preferred Size'. With this settings height of text and rect size computed automatically
     
    Simie likes this.