Search Unity

[Solved] Help with layout element constraint

Discussion in 'UGUI & TextMesh Pro' started by Thibault-Potier, May 1, 2019.

  1. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Hi
    I'm trying to make a messaging app.
    I'm currently building the conversation screen behaviour.
    I have reach the point where i can add new message on a scroll rect viewport with a vertical layout group on the content holder.
    Spawning new message on this list is working fine, but now i want the messages to expand their content size based on the text inside.

    I got it to work like this :
    1) all the messages have the same fixed width, and height will expand depending of the text lengh
    or :
    2) the width of the message is growing with the text lengh, but this width won't be limited to a max width

    As an illustration :
    solution 1 )


    Solution 2 )


    I would like to have a mix of this 2 behaviour: the size of the message should fit the text lengh until it reach a max size, and then just expand in height.

    I tried several stuff, and i'm now getting confuse in the correct way to do it. How can i achieve it ?
    here are other inspector hierarchy info :


     
    Andrew-Chewie likes this.
  2. nickfagan

    nickfagan

    Joined:
    Oct 27, 2015
    Posts:
    4
    Hey there! I figured it out, here's how you can do it:

    1. On the "Content" object, on Vertical Layout Group, select Control Child Size: Width and Height, leave everything else un-selected. On the Content Size Fitter, set horizontal to un-constrained and vertical to preferred size:

    Screen Shot 2019-05-02 at 12.52.15 PM.png

    2. On each list item (MsgPrefab in your case) add a Vertical Layout Group and set it to the same as the Content object (Control Child Size: Width and Height):

    Screen Shot 2019-05-02 at 1.02.28 PM.png

    3. Remove any layout components from the Text object:

    Screen Shot 2019-05-02 at 1.05.21 PM.png

    That should do it :)
     
  3. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    oh thx ! I will test this tonight
     
  4. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    That works perfectly ! Thanks a lot
    I lack knowledge about unity ui components and was pulling my hairs on this. I should find a good unity ui courses to master this kind of stuff
     
  5. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    I don't want to abuse, but do you have an idea for an elegant solution to display the msg on the left - or right - side of the screen ( to make clear differences between the user own messages and those he received).

    right now i'm trying this setup :





    I set the padding value manually in this screenshot, but i would end up making a script to set a value in padding left / padding right of the content gameobject depending of the message width but it seems like an over complex solution.
    Moreover, i'm nesting vertical layout group inside vertical layout group and this layoutception seems like a suspect solution.
     
  6. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Years later, and this single post has solved a problem that has taken me days to find the answer for! A thanks to you!