Reputation: 83
What up.
I'm instantiating a textbox gameobject. Then filling it up with text.
Any way to make the textbox height dynamically change?
GameObject reply1 = Instantiate(replyText, transform.position,
transform.rotation);
reply1.transform.SetParent(textArea.transform, false);
Text reply1text = reply1.GetComponent<Text>();
reply1text.text = gameRounds[roundCount].reply[0];
The width is fine, don't need to change it.
Upvotes: 4
Views: 17227
Reputation: 51
I need to correct @gmspacex's answer, which is utterly wrong (the screenshot speaks for itself, showing a warning), for future reference. The correct way of setting a Text element with a dynamic height or width is clearly described here: Make children of a Layout Group fit their respective sizes
To summarize you just need to:
No shenanigans with toggling the component on and off. If you find some element being "squashed", just add a LayoutElement component and set the Min Height property appropriately.
screenshot of the correct way of setting up the vertical layout group component
Upvotes: 5
Reputation: 640
You can add the ContentSizeFitter component to the text gameobject, and then set Vertical Fit to the Prefered Size. This solution only controls the text box's height.
If you want to control the parent's height automatically, follow those steps.
{
exampleText.text = "Text Example \n Test automatic height";
Invoke("UpdateParentLayoutGroup", 0.1f);
}...
void UpdateParentLayoutGroup() {
exampleText.gameObject.SetActive(false);
exampleText.gameObject.SetActive(true);
}
Upvotes: 2
Reputation: 1601
You can change the text box's Vertical Overflow settings to Overflow. It automatically increases the textboxes height based on text content.
Upvotes: 0