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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How to prevent scroll from moving on text update?

Discussion in 'UGUI & TextMesh Pro' started by kostic017, Aug 20, 2020.

  1. kostic017

    kostic017

    Joined:
    May 25, 2019
    Posts:
    9
    I have a TMP_InputField with a vertical scrollbar.

    I'm changing the text of the field while the player types with the purpose of highlighting some words etc.

    Here is some sample code.
    Code (CSharp):
    1. public class NewBehaviourScript : MonoBehaviour
    2. {
    3.     TMP_InputField input;
    4.  
    5.     void Start()
    6.     {
    7.         input = GetComponent<TMP_InputField>();
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         input.text = Regex.Replace(input.text, "00000", "11111");
    13.     }
    14. }
    When I change something in the text, the scrollbar automatically positions itself so that the changed line is on the bottom of the input field (like in the GIF), which is annoying.

    I've tried bringing back the scrollbar to the old position, but this did nothing:
    Code (CSharp):
    1. float scroll = input.verticalScrollbar.value;
    2. input.text = Regex.Replace(input.text, "00000", "11111");
    3. input.verticalScrollbar.value = scroll;
     

    Attached Files:

  2. kostic017

    kostic017

    Joined:
    May 25, 2019
    Posts:
    9
    As far I can tell when I do
    input.text = ...
    , scroll value changes to 0 and then changes again based on the caret position. Is there any way to prevent this recalculation, or to at least bring back the old value?
     

    Attached Files: