- Home /
4.6 UI Text rect does not expand automatically
Hello,
I have a window that has a lot of text inside but the text is filled in dynamically. The problem is that the rect of text object does not grow to accommodate the text which makes text scrolling not work.
Am I doing something wrong or is it a bug? Or is that intentional?
I'm using unity 4.6.0b18
Thanks a lot
I'm a bit confused here as to what you want to say here:
The problem is that the rect of text object does not grow to accommodate the text which makes text scrolling not work.
The way it works is, if the rect of text grows to fit the text then there will be no scrolling and if you want scrolling then the rect should not grow.
Could you specify which one out of these two effects do you want to achieve?
$$anonymous$$y hierarchy is like this "Canvas>Panel>Text" Panel has Scroll Rect attached that references Text. When Text's rect size is same as panel or smaller, no scrolling occurs. When Text's rect is bigger than panel, it scrolls as intended. But adding more text does not make Text's rect increase in size, thus if there is too much text to fit in Panel, it will not show but it also won't scroll since Text's rect is not out of Panel's bounds to start scrolling.
When I manually size Text's rect big, it scrolls fine but it isn't dynamic. If there's a lot of text, Text's rect does not increase in size to accommodate said text, thus not triggering scrolling.
Did you ever find a solution? I'm trying to figure out the same thing right now.
I have the same issue in Unity 5.5.0f1. It use to work on 5.5.0b3. Did you manage to fix it? Thanks :)
Answers should be used for answers. Not questions. Please use the comment feature in the future. Thank you.
Answer by Ash-Blue · Oct 06, 2014 at 09:14 PM
You will want to use the "content size fitter" component as mentioned by @Morning. Example image included.
though the accepted answer may work (i have not checked it)... this answer seems to be the better way to do it...
Answer by pickle chips · Sep 14, 2014 at 10:26 PM
In case you're still needing this, I Just got this working with this little script that you can attach to the Text object. The Text component luckily has a property called "PreferredHeight" which is just the height of the overflowing text. So set the height of the textbox to the preferred height and boom you're golden.
RectTransform rt;
Text txt;
void Start () {
rt = gameObject.GetComponent<RectTransform>(); // Acessing the RectTransform
txt = gameObject.GetComponent<Text>(); // Accessing the text component
}
void Update () {
rt.sizeDelta = new Vector2(rt.rect.width, txt.preferredHeight); // Setting the height to equal the height of text
}
And that's all it takes.
That works but turns out there is a "content size fitter" component that seems to do the same
I prefer pickle chips' method, as the Content Size Fitter caused undesired behavior.