Search Unity

Bug TMPInputField + CustomValidator + iOS/Android build = super strange behaviour

Discussion in 'UGUI & TextMesh Pro' started by FernwehSmith, Jul 14, 2022.

  1. FernwehSmith

    FernwehSmith

    Joined:
    Jan 31, 2022
    Posts:
    36
    Hi All,
    I've got a weird bug where any TMP Input Field that has a custom validator attached to it causes this weird behaviour when built to either Android or iOS.
    Nature of the behaviour:
    • Duplicates the existing text
    • Won't let me delete it
    • Only happens with Custom Validators AND iOS/Android Builds. Testing in editor or just choosing one of the pre-set validators does not produce this issue.
    Notes about the project and what I've tried:
    • Making an AR project with AR Foundation.
    • The input field pictured in the video is as default as you can get. Not apart of any prefabs and no custom scripts affecting it.
    • Tried both my own as well as unity's example validators. Behaviour is produced with all custom validators.
    • Testing in the editor works exactly as you would expect.
    Any guidance on this issue would be super helpful!
     
  2. Romilk31

    Romilk31

    Joined:
    Mar 22, 2019
    Posts:
    2
    Bump. Facing the issue with TextMeshPro 3.0.6 and unity 21.3.26f1.

    Edit: Fixed it by upgrading to preview TextMeshPro version 3.2.0-pre.4
     
    Last edited: Jun 29, 2023
  3. ryan-at-melcher

    ryan-at-melcher

    Joined:
    Oct 22, 2018
    Posts:
    16
    Here's a fix, assuming that your custom input validator modifies its text ref argument directly:

    Change the return value so it always returns '\0'.

    Code (CSharp):
    1. public override char Validate(ref string text, ref int pos, char ch)
    2. {
    3.     text = SomeFunctionThatFormatsOrValidatesText(text);
    4.  
    5.     // Always return NUL to avoid concatenating this character onto `text`.
    6.     // Concatenation isn't needed here because `text` has already been formatted.
    7.     return '\0';
    8. }
    9.  
    By returning '\0', we're telling TextMesh Pro not to append any characters to text which, in this case, is exactly what we want because we've already decided and overwritten text's value.

    For example, my validator formats a phone number "(###) ### - ####" and needs to insert the parenthesis, hyphen and spaces. Because these characters are not entered by the user, they can only be added via assigning to the text argument directly.

    This is not a perfect solution because TMP_InputField will still call the validator for every character in the input field's text (i.e., repeatedly formatting the string even though one time was sufficient). But this will solve the duplicate characters issue and inability to backspace the input entirely on Android and iOS.
     
    Last edited: Apr 22, 2024 at 11:10 PM