Search Unity

TextMeshPro TextInfo CharacterInfo from source

Discussion in 'UGUI & TextMesh Pro' started by cantfindanamethatisntalreadytaken, Nov 9, 2018.

  1. cantfindanamethatisntalreadytaken

    cantfindanamethatisntalreadytaken

    Joined:
    Sep 21, 2018
    Posts:
    4
    Hi I'd like to get CharacterInfo about a position in the TextMeshPro text and I can't see how to do it.

    Given the text is "<b>A</b>" the text length is 8 and textInfo reports characterCount is 2, I can find the textInfo about characterInfo[0] and it correctly returns "A"

    The length of the text is longer than the characterCount because of the tags used.

    What I want to do is find it from the index of the text not the resulting textMesh. In this example, I want to find the textInfo about text[4] which is, again "A"

    Is there any way, knowing the position in the original text, what the chracterInfo is based on the character position in the text?

    I can see ways to find text based on mouse positions etc but can't see how to do this.

    Thanks
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    CharacterInfo contains information about the characters in the text. These are the visible characters and non-visible characters like white spaces. This excludes all rich text tag / meta data.

    The CharacterInfo buffer is allocated in blocks and as such its length will rarely match the number of character in the text layout. Information about the actual results of the layout of the text including character, word, line, etc count is contained in the textInfo.

    The CharacterInfo contains data about each character including an index which is the index of the character in the original string.

    The textInfo and its content is used in many examples provided with TextMesh Pro. To learn more about how to interact with element of the text. See example 12 and 12a and the relevant scripts. These are provided as examples and should be a good source to learn from.
     
  3. amateurd

    amateurd

    Joined:
    Nov 1, 2016
    Posts:
    97
    Hi thanks Stephan.

    I've got it sort-of working by holding a second array of integers for the original (pre-rendered) text, where each integer matches the position in the rendered text. Each time the text changes, I loop through the rendered text characters and update the array.

    A bit like CharacterInfo but the lookup is the other way around.