Search Unity

Unity UI Scroll and child with dynamic size

Discussion in 'UGUI & TextMesh Pro' started by TrueNoob141, Sep 18, 2019.

  1. TrueNoob141

    TrueNoob141

    Joined:
    Sep 18, 2019
    Posts:
    30
    How to create something like this (without code)?
    Code (csharp):
    1. scroll (Scroll Rect)
    2. -viewport (Mask)
    3. --content (ContentSizeFitter + VerticalLayoutGroup)
    4. ---item.prefab (VerticalLayoutGroup)
    5. ----background
    6. ----title_label (LayoutElement with preffered size)
    7. ----child_items (LayoutElement? + VerticalLayoutGroup)
    8. -----item1
    9. ------left_label (any text/multiline)
    10. ------right_label (one line)
    11. -----item2
    12. -----...
    13. -----itemN
    screen.png
     
    Last edited: Sep 19, 2019
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Sounds like you already have a fairly specific idea of how to create it using standard Unity UI components. What trouble are you running into?

    (One hole I notice in your description is that the "background" should probably have a LayoutElement component with the "ignore layout" box checked.)
     
  3. TrueNoob141

    TrueNoob141

    Joined:
    Sep 18, 2019
    Posts:
    30
    left_label doesn't scale item. If add ContentSizeFitter I get something like this
    screen2.png
     
    Last edited: Sep 18, 2019
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    OK, so your problem is that your ContentSizeFitter isn't correctly determining the preferred size of your text? Could you describe exactly what components you have attached to left_label?
     
  5. TrueNoob141

    TrueNoob141

    Joined:
    Sep 18, 2019
    Posts:
    30
    Preffered size of text is correct (with size fitter), but parent (child_items and item.prefab) dont change.
    left_label has only TMP UGUI and I trying attach ContentSizeFitter
     
  6. TrueNoob141

    TrueNoob141

    Joined:
    Sep 18, 2019
    Posts:
    30
    I want to display text of any size in runtime, but without custom ui code
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It looks like child_items has both a vertical size fitter AND a layout element? Probably the layout element is preventing it from resizing, because it thinks you want it to stay at the size specified there. Try removing the layout element from child_items?
     
    TrueNoob141 likes this.
  8. TrueNoob141

    TrueNoob141

    Joined:
    Sep 18, 2019
    Posts:
    30
    It work. Thanks! But sorry, I give the wrong hierarchy. :( Can you help with it (edited: added item in child_items)?

    In edited case I just have list of items with bg and two columns.
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Perhaps "item" can use a Horizontal Layout Group for laying out its two columns?
     
    TrueNoob141 likes this.
  10. TrueNoob141

    TrueNoob141

    Joined:
    Sep 18, 2019
    Posts:
    30
    Wow! That works! Thanks!

    Code (csharp):
    1. child_items (Layout Element + Vertical Group with control size)
    2. -item (only Horizontal Group with control size)
    3. --left_label (only TMP)
    4. --right_label (only TMP)