Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

TextMesh Pro Want to enable Input Field vertical scrollbar when necessary

Discussion in 'UGUI & TextMesh Pro' started by kryzodoze, May 1, 2018.

  1. kryzodoze

    kryzodoze

    Joined:
    Nov 6, 2013
    Posts:
    21
    Is there a way to tell the input fields that we only want the scrollbar to be visible when the user has typed enough text such that it's needed? Right now it shows up at all times.

    Looking for similar functionality to how you can put a normal TMP object into a unity scroll rect, and as the text increases in size, the scroll rect's scrollbar will enable after a certain threshold.
     
  2. kryzodoze

    kryzodoze

    Joined:
    Nov 6, 2013
    Posts:
    21
    Had to do this the old-fashioned way. So far it works for me. Here's roughly what I'm doing:

    Code (CSharp):
    1. StartCoroutine(this.UpdateAnswerScrollbarVisibility())
    2.  
    3.  
    4. private IEnumerator UpdateAnswerScrollbarVisibility()
    5. {
    6.             // This needs to be delayed a frame because it relies on the rect
    7.             // transform height which isn't always up to date because of the
    8.             // dynamic layout.
    9.             yield return new WaitForEndOfFrame();
    10.  
    11.             var preferredHeight = this._answerInput.textComponent.preferredHeight;
    12.             var currentHeight = this._answerInputRectTransform.rect.height;
    13.             this._answerInput.verticalScrollbar.gameObject.SetActive(
    14.                 preferredHeight >= currentHeight);
    15. }
    I call it after the user has typed text, which you can hook into by adding a listender to the input field's 'onValueChanged' event handler.
     
    ltsb and karmatha like this.