Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug Input Field Layout does not Rebuild on Value Change

Discussion in 'UGUI & TextMesh Pro' started by dominikkoller, Apr 4, 2024.

  1. dominikkoller


    Jul 26, 2018
    There seems to be a bug in TMP Input Field. The layout does not update on text input.

    • In a canvas, have a PARENT gameobject with a VerticalLayoutGroup.
    • For width and height, check Control Child Size and Use Child Scale, and uncheck Child Force Expand.
    • As a child of PARENT, put a new Input Field gameobject via Right Click - UI - Input Field Text Mesh Pro.
    • In the input field, use Line Type Multi Line Newline
    • In the text inside, make sure Wrapping is enabled
    Now start the application, and write some text. When a new line is reached via wrapping or entering a newline, the Input Field does not resize vertically to fit the text.

    Expected behaviour would be that it does rezise. In the Editor, if you resize the window, the input field actually does resize properly. You can see in the inspector, if you select LayoutProperties at the bottom, that the PreferredHeight given by the InputField does not change until there is some manual trigger like the window resize. Thus expected behaviour would be for the InputField to update the LayoutProperties on input change.

    I created a component that triggers UI Refreshes:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    4. public class ManualLayoutRebuilder : MonoBehaviour
    5. {
    6.     public void MarkLayoutForRebuild()
    7.     {
    8.         LayoutRebuilder.MarkLayoutForRebuild(GetComponent<RectTransform>());
    9.     }
    10. }
    Attach it to the InputField GameObject, and on the Input Field go to On Value Changed - (+) - add the ManualLayoutRebuilder.MarkLayoutForRebuild function to that event.

    Now the layout will be properly rebuilt when the input changes.

    InputField should do exactly this by itself.