C# Unity UI文本框动态高度。如何使其基于文本量展开?

C# Unity UI文本框动态高度。如何使其基于文本量展开?,c#,user-interface,unity3d,C#,User Interface,Unity3d,怎么了 我正在实例化一个textbox游戏对象。 然后用文本填充它 有没有办法让文本框的高度动态变化 GameObject reply1 = Instantiate(replyText, transform.position, transform.rotation); reply1.transform.SetParent(textArea.transform, false); Text reply1text = reply1.GetComponent<Text>(); reply1

怎么了

我正在实例化一个textbox游戏对象。 然后用文本填充它

有没有办法让文本框的高度动态变化

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];
GameObject reply1=实例化(replyText,transform.position,
变换(旋转);
reply1.transform.SetParent(textArea.transform,false);
Text replytext=reply1.GetComponent();
reply1text.text=gameRounds[roundCount]。回复[0];

宽度很好,无需更改。

您可以将文本框的垂直溢出设置更改为溢出。它会根据文本内容自动增加文本框的高度。

您可以将contentSizeFile组件添加到文本游戏对象,然后将垂直适合设置为首选大小。此解决方案仅控制文本框的高度

如果要自动控制父对象的高度,请执行以下步骤

  • 将垂直布局组添加到父级
  • 添加文本游戏对象作为子对象,并添加内容大小调整器组件
  • 现在尝试更改脚本中文本组件的文本。你会注意到它不能正常工作。您应该手动更新父级的布局组
  • 享受团结
    这只会溢出文本,使其在维度之外继续。它不会按照OP的要求更改文本框的高度。如果您希望动态更新大小以适应某个区域(即,随着添加更多文本而增长的滚动区域),则这将不起作用。
    {
         exampleText.text = "Text Example \n Test automatic height";
         Invoke("UpdateParentLayoutGroup", 0.1f);
    }...
    
    void UpdateParentLayoutGroup() {
         exampleText.gameObject.SetActive(false);
         exampleText.gameObject.SetActive(true);
    }