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.
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With