- Home /
How to prevent scroll from moving on text update?
I have a TMP_InputField with a vertical scrollbar and 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:
public class NewBehaviourScript : MonoBehaviour
{
TMP_InputField input;
void Start()
{
input = GetComponent<TMP_InputField>();
}
void Update()
{
input.text = Regex.Replace(input.text, "00000", "11111");
}
}
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:
float scroll = input.verticalScrollbar.value;
input.text = Regex.Replace(input.text, "00000", "11111");
input.verticalScrollbar.value = scroll;
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?
Answer by logicandchaos · Aug 30, 2020 at 02:51 PM
You can save the value in a variable and reset it, like int tempCaretPos;
The question is about scrollbar value. And as I said, I tried resetting scrollbar value, but initially without any success.
Your answer
Follow this Question
Related Questions
Text Mesh Pro Input field cannot scroll to last line of inserted text. 0 Answers
How can I create a Multi line Input Field with Scrollbar? 0 Answers
Allow TMP_InputField to show HTML Tags even Rich Tags are ENABLED. 0 Answers
Textmesh Pro Input Field Rich Text 3 Answers
How to create a scrollable inputfield 3 Answers