Search Unity

Layout Groups: Controlling Child Size

Discussion in 'UGUI & TextMesh Pro' started by Inxentas, Feb 11, 2020.

  1. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    278
    Hello everyone. I'm trying to get something done in the UI with a Vertical Layout Group / Content Size Fitter, and bashing my head against an aspect I'm probably not approaching from the right angle.

    The idea is simple: make a large scrollable area that can contain a block of text. Below the text are buttons which need to scroll along with the text. The text is a TextMesh(pro) and the Buttons are Prefabs that get instantiated every time the player selects a response (and the next dialogue is shown).

    The Vertical Layout group is set to control child width and this works nicely. I would like it to control height also, because the text may be one or two lines or a whole paragraph of exposition. This also works nicely as the buttons are cleanly placed below the text, but their internal TextMesh(pro) is affected.

    In the end I would also like to have my buttons be able to display multiple lines without any components screwing with the text size. So in effect, I would like the buttons to dictate their own height but not the text, since the amount of lines and buttons will vary greatly. The reason I wish to put both in the same scrollable area is because I'm going for that Baldurs Gate aesthetic, but would like to use buttons for my response lines to keep gamepad UI control possible.

    EDIT: I've since figured out my main issue is that I can't get a TextMeshProUGUI to automaticly adapt to it's own preferred size. Is there a way I can do that programmaticly? I've been trying to modify it's RectTransform and Rect components but they won't let me set the values I'd need to change to the textmesh's preferred size. I can change the TransformRect's height in the editor and the preferred sizes I Debug.Log() match perfectly with what I want. I just can't overwrite the Rect, RectTransform, or any of their variables. Is there a way around this? I've look at Editor options and tried making a Content Size Fitter affect just the text at the top with Preferred Size but that was no solution (and gave me warnings because it can't be nested with anything that has a Vertical Layout Group.
     
    Last edited: Feb 11, 2020