Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

TextMesh Pro TMP Bug - Custom Validator Text Selection

Discussion in 'UGUI & TextMesh Pro' started by BodieHookbang, Aug 7, 2020.

  1. BodieHookbang

    BodieHookbang

    Joined:
    Jun 18, 2019
    Posts:
    1
    Hello there!

    Ive noticed a possible bug with TMP regarding custom input validators on TMP_InputField. When using a custom validator of any kind, the functionality of selecting/highlighting a range of text doesn't work correctly.

    Expected: The user selects a range of text. When the user types the next character, the entire selected area of text is deleted and replaced with the new character (assuming it passes the custom validator).

    What actually happens: The user selects a range of text. When the user types the next character, the selected text is not removed. The new character is added wherever the cursor last was (usually at the beginning or the end of the text selection). The range of text you previously selected becomes unselected.


    EDIT: After further investigation, there is more to the bug than I thought. After building and testing on android, tapping the input field no longer brings up the keyboard.

    (Unity version 2019.11.3f1)
    (TMPro 2.0.1)
    Here is the custom validator I was using. Maybe im using this wrong? I don't know.
    Code (CSharp):
    1.   [CreateAssetMenu(fileName = "ScreenNameValidator", menuName = "TextMeshPro/Text Validator/Screen Name Validator", order = 1)]
    2.   public class TMP_ScreenNameValidator : TMP_InputValidator
    3.   {
    4.     /*CHARACTERS ALLOWED
    5.      * Uppercase and lowercase letters
    6.      * numbers 0-9
    7.      * underscore
    8.      */
    9.  
    10.     public override char Validate(ref string text, ref int pos, char ch)
    11.     {
    12.       if(char.IsLetterOrDigit(ch) || ch == '_') {
    13.         text = text.Insert(pos, ch.ToString());
    14.         pos += 1;
    15.         return ch;
    16.       }
    17.       else {
    18.         return '\0';
    19.       }
    20.     }
    21.   }

    I'm not sure if you are already aware of a bug like this, but I thought i'd at least put it here to be safe. Ive never posted on these forums before...
     
    Last edited: Aug 7, 2020
  2. obulka

    obulka

    Joined:
    Dec 24, 2017
    Posts:
    1
    Hello,

    I just ran into this issue as well and was able to fix it by adding the following script to the TMP_InputField's game object.

    Code (CSharp):
    1. using System;
    2. using TMPro;
    3. using UnityEngine;
    4.  
    5.  
    6. [RequireComponent(typeof(TMP_InputField))]
    7. public class TMPHighlightFix : MonoBehaviour
    8. {
    9.     private TMP_InputField inputField;
    10.  
    11.  
    12.     protected void Start()
    13.     {
    14.         inputField = GetComponent<TMP_InputField>();
    15.  
    16.         inputField.onValueChanged.AddListener(
    17.             delegate {
    18.                 OnValueChanged();
    19.             }
    20.         );
    21.     }
    22.  
    23.     private void OnValueChanged()
    24.     {
    25.         int caretPosition = inputField.caretPosition;
    26.         int selectionPosition = inputField.selectionAnchorPosition;
    27.         if (caretPosition != selectionPosition)
    28.         {
    29.             int startIndex = Math.Min(caretPosition, selectionPosition);
    30.             int numToRemove = Math.Abs(caretPosition - selectionPosition);
    31.             inputField.text = inputField.text.Remove(
    32.                 startIndex,
    33.                 numToRemove
    34.             );
    35.             inputField.caretPosition = startIndex + 1;
    36.         }
    37.     }
    38. }
    39.  
     
    Olipool likes this.