- Home /
UI Vertical Layout Group with Content Size Fitter, how to set max width?
Hello guys! I need a box that will expand as text is growing, so width of box is growing too, but i how can i limit the expanding width, so my boxes width wont be more than screen width?
Im using Vertical Horizontal Layout Group and everything works as i want, but i dont know how to limit the width of a main box?
I tryed to change preferredWidth, but this seems to be buggy.
public int maximumWidth = 100;
void Awake()
{
if (GetComponent<RectTransform>().rect.width > maximumWidth)
{
GetComponent<LayoutElement>().preferredWidth = maximumWidth;
}
}
Answer by LK84 · Dec 16, 2016 at 05:25 PM
It seems like you set the anchor points to something like xmin=xmax=0.5. To make the box stay within the canvas enter xmin=0 and xmax=1
I dont understand where should i change anchors points?
https://docs.unity3d.com/$$anonymous$$anual/class-RectTransform.html
You can either type in the Anchor points manually or use one of the anchor presets (middle stretch is the one that suits you most I think),