Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3D - "Parent has a type of layout group" error

I have this simple structure inside a Canvas:

  • GameObject (Vertical Layout Group)
    • UI Image (Vertical Layout Group)
      • TMP UGUI Text (Horizontal Layout Group, Content Size Fitter)

Now, it works pretty well, when the text gets out of boundaries, the UI-Image expands. But since I have a Layout Group as a parent to my TMP Text, I get an error inside the Text, in the Content Size Fitter, saying that 'The parent has a a type of layout group component...'. And as a result, the image height sometimes doesn't update when I the text changes (doesn't grow or shrink, only after I refresh or save the project).

enter image description here enter image description here

Am I doing something wrong here?

like image 404
sisi mendel Avatar asked Oct 30 '18 07:10

sisi mendel


1 Answers

I will convert this to answer since I'm going to attach images. I did something similar to this for a chat feature.

  1. Make the parent have the content size fitter and set either the horizontal or vertical fit to preferred size. ContentSizeFitter in Parent

  2. Let its immediate child be the background, and add a layout element to it to control its minimum size. Also add a layout group to it to control the text that you will put as a child to it. BackgroundMinSize

  3. Add the text as a child of background CurrentText

  4. Change the text to check the background. ExpandedChild

[EDIT] 1. I changed the Parent's layout group to VerticalLayoutGroup. 2. I made a duplicate of the game object "Background" but with different texts to that you can see how the ContentSizeFitter and VerticalLayoutGroup controls the size of the children. DuplicateGameObjects Result

In the screenshots above, I never adjusted anything on the rect transform. I just changed the text, and as you can see, the background of each text/dialog/message also adjusted.

like image 180
Tricko Avatar answered Oct 29 '22 01:10

Tricko