Search Unity

TextMesh Pro InputField.caretPosition gets overwritten when field is activated

Discussion in 'UGUI & TextMesh Pro' started by joonturbo, Feb 28, 2019.

  1. joonturbo

    joonturbo

    Joined:
    Jun 24, 2013
    Posts:
    77
    I have an on-screen button to add a special character to a TMP_InputField
    I can add the character it in the right place (by disabling resetOnDeActivation, the caretPosition is still readable after focus is lost), but when I add the character, do caretPosition++, and call ActivateInputField(), it resets to the previous caret position.

    Code (CSharp):
    1. void InsertCharacter(TMP_InputField myField, string specialChar) {
    2. myField.text = myField.text.Insert(myField.caretPosition, specialChar);
    3. myField.caretPosition++;
    4. myField.ActivateInputField();
    5. }
    The only way I found around it is to wait 1 or 2 frams before setting the caret position, but it causes at least one blink in the wrong position, which makes for bad UX.

    I've done a Debug.Log on _lastField.caretPosition in Update() which confirmed my findings:

    Code (CSharp):
    1. position: 4
    2. position: 4
    3. Insert Character
    4. position: 5
    5. position: 4
     
    Last edited: Feb 28, 2019
    abishekhparivel likes this.
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I just tried the above in version 1.4.0-preview.2a and it behaves as expected.

     
  3. Jackrohit

    Jackrohit

    Joined:
    Mar 29, 2019
    Posts:
    10
    @Stephan_B Caret position is not correct when I click on Inputfield but works well as soon as I started typing.
     

    Attached Files:

  4. gamefox87

    gamefox87

    Joined:
    Aug 19, 2016
    Posts:
    64
    @Stephan_B To help you reproduce the bug. Create any input field and make it center aligned. Click on it. The caret position will be centered. Then start typing and press "backspace". Then press enter to send a message and click again in input field, the caret position will be left aligned. Basically, the caret position jumps from center to left and it's something to do with backspace.
     
  5. meghnazala746

    meghnazala746

    Joined:
    Dec 28, 2020
    Posts:
    8
    how do you do this?