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. Dismiss Notice

TMP_CharacterInfo.index mismatch with ITextPreprocessor

Discussion in 'UGUI & TextMesh Pro' started by kristofferlundberg, Nov 2, 2020.

  1. kristofferlundberg

    kristofferlundberg

    Joined:
    May 7, 2020
    Posts:
    3
    Hi,

    I'm currently implementing custom tags functionality for TextMeshProUGUI, but I've stumbled upon a problem.
    I'm using ITextPreProcessor and in the implemented method "PreprocessText(string text)" I look for my custom tags in the text, store them for further use, then return the text without said tags. This works perfect, but the indices generated are not correct.

    Let me show you two examples.

    Text with built-in tag:

    <color=red>Hello</color>


    The first element in TMP_CharacterInfo array is the letter "H" with index 11. This is correct!

    Text with custom tag:

    <shake=10>Hello</shake>


    The first element in TMP_CharacterInfo array is the letter "H" but with index 0. I'm expecting 10 here, since the text input still contains the custom tag, only not displaying it to the user.

    Am I completely in the wrong here about how it should work or can someone else confirm if this is a bug and if it can be fixed?

    Thanks in advance!
    / Kristoffer
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    The ITextPreProcessor and more specifically the PreprocessText() function is called before the text is processed to provide for an opportunity to modify the incoming text. As such, the TextInfo and sub structures like characterInfo have not been populated / updated yet.
     
  3. kristofferlundberg

    kristofferlundberg

    Joined:
    May 7, 2020
    Posts:
    3
    Ok I see!
    Do you have any suggestions on how to deal with this?

    Thanks!
     
  4. celinedrules

    celinedrules

    Joined:
    Jan 31, 2013
    Posts:
    16
    I know this is old but have you tried to use regular expressions?

    Code (CSharp):
    1. string pattern = @"(?<=<color.*?>)(.*?)(?=<\/color>)";
    2. string text= "<color=red>Hello</color>";
    3. Match match = Regex.Match(text, pattern);
    4. Debug.Log(match);
    This will return Hello
     
  5. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    983
    And this "ITextPreProcessor" interface goes on what class?
    A custom "TextMeshProUGUI" class, a custom MonoBehaviour that lives on the same gameObject? There are zero docs for this I can find.