Search Unity

Question Replace characters in InputField as soon as typed

Discussion in 'UGUI & TextMesh Pro' started by llermy, May 16, 2023.

  1. llermy

    llermy

    Joined:
    Jul 22, 2020
    Posts:
    3
    Hi!

    I'm using TextMeshPro InputField and I wanted to replace some of the characters the user types on the fly. With the OnValueChanged event we can get the new inputfield text string whenever it changes. However it seems there is no method that provides the index of the last change in the inputfield text string. I could save last inputfield text string and traverse the new string to check the first different char occurrence each time the OnValueChanged event is fired and replace it if it is among the characters to replace, but traversing the whole string every time the user types something is not at all efficient performance-wise. Is there a better, more performant way of doing it?
     
  2. llermy

    llermy

    Joined:
    Jul 22, 2020
    Posts:
    3
    Okay I found a solution. Just use
    inputField.stringPosition
    so you know at what position in the string the caret is now and you know what was the last character. :)