Search Unity

TMP_InputField set caretPosition regression

Discussion in 'UGUI & TextMesh Pro' started by dec-dev0, Apr 15, 2021.

  1. dec-dev0

    dec-dev0

    Joined:
    Mar 21, 2014
    Posts:
    15
    Using version 2.1.4:

    I'm trying to add a character separator dynamically while the player is typing but its not working anymore.
    eg: The user write `ABCDEFGH`, it outputs `ABCD-EFGH
    Impossible to moved the caret after the last character (until canvas redraw ?).

    To reproduce:

    The `OnValueChange` is attached to the InputField
    Code (csharp):
    1.  
    2.     public void OnValueChange(string value)
    3.     {
    4.         if (inputField.text.Length == 4)
    5.         {
    6.             Debug.Log(inputField.caretPosition);
    7.             inputField.SetTextWithoutNotify(inputField.text + '-');
    8.             // Increase Caret position
    9.             inputField.caretPosition = inputField.text.Length + 1;
    10.             Debug.Log(inputField.caretPosition);
    11.         }
    12.     }
    13.  
    Console Output:

    4
    5



    Visual Output:
    sshot-182.jpg


    The same code was working perfectly before (I'm not able to remember if TextMeshPro was already on the Package Manager at that time).
     
  2. dec-dev0

    dec-dev0

    Joined:
    Mar 21, 2014
    Posts:
    15
    @Stephan_B, is there a way to force a Rebuild call to `UpdateGeometry` ?
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I'll try taking a look this afternoon and follow up as soon as I have more information.
     
  4. dec-dev0

    dec-dev0

    Joined:
    Mar 21, 2014
    Posts:
    15
    @Stephan_B, did you have some time to look for this issue ?
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Have you tried the following?

    Code (csharp):
    1.  
    2. void OnValueChange(string text)
    3. {
    4.     if (text.Length == 4)
    5.     {
    6.         InputFieldComponent.SetTextWithoutNotify(text + '-');
    7.         InputFieldComponent.MoveTextEnd(false);
    8.     }
    9. }
    10.  
    P.S. I did have a chance to look into the caret position not updating as expected and have resolved that issue.
     
    Last edited: Apr 23, 2021
  6. dec-dev0

    dec-dev0

    Joined:
    Mar 21, 2014
    Posts:
    15
    Same issue with MoveTextEnd.
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    See if making the following changes resolves the issue on your end.

    upload_2021-4-26_1-35-23.png

    Note the lines numbers might be different depending on the version of the TMP package you are using.

    In order for these changes to be persistent, you will need to make them in the Global Package Cache.
     
  8. dec-dev0

    dec-dev0

    Joined:
    Mar 21, 2014
    Posts:
    15
    Yes, thank you, it is fixing the issue.

    For the record, I identified another bug with the same sample code even after applying your fix.
    If you paste the content of a string with the same code, `onValidateInput` fails because `insertionPosition` is not correctly updated with the new input.text content that was updated in the `onValueChanged`.

    I'm attaching a project to reproduce:

    string to enter: ABCDEFGHIJKLMNOPQRST
    • result when entering each character one by one: ABCDE-FGHIJ-KLMNO-PQRST
    • result when pasting the same string: ABCDEFGHIJKLMNOPQRST-
     

    Attached Files: