Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

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

  1. FernwehSmith


    Jan 31, 2022
    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


    Mar 22, 2019
    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


    Oct 22, 2018
    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);
    5.     return '\0';
    6. }
    This solves the issue because Validate is invoked after `ch` has already been appended to `text`. By returning '\0', we prevent TextMesh Pro from appending extraneous characters.

    My validator formats a phone number "(###) ### - ####" and needs to insert the parenthesis, hyphen and spaces. These characters cannot be entered by the user and therefore they can only be added by assigning to the text argument directly.

    There is some wasted cycles with this approach. TMP_InputField calls the validator for every character in the input field's text. This means `text` is repeatedly formatted/reassigned even though one pass is likely sufficient. However, this does solve the duplicate characters issue and inability to backspace the input entirely on Android and iOS.
    Last edited: May 13, 2024