Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question textInfo.characterInfo.Length always returns 256

Discussion in 'UGUI & TextMesh Pro' started by unity_zXeR0BHpYJM_Tw, Aug 5, 2022.

  1. unity_zXeR0BHpYJM_Tw

    unity_zXeR0BHpYJM_Tw

    Joined:
    Mar 27, 2020
    Posts:
    5
    Long story short, for some reason, at a point, I need to retrieve the characterInfo with given index numbers. I have at least 600 characters in my textMeshProUGUI text but if I try to fetch characterInfo of the 333rd one (tmp.textInfo.characterInfo[333]) it doesn’t work because the tmp.textInfo.characterInfo.Length is always 256.


    tmp.textInfo.characterCount returns the right amount, but I can’t access all of them if the index is more than 256.

    Any suggestions?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The text processing data structures such as TextInfo and sub structures like characterInfo, etc. are allocated in blocks in power of two. For examples, if the text only contained 23 characters, the characterInfo array length would be of size 36.

    The textInfo count properties reflect the number of characters, words, spaces, lines, etc. These should be queried for iterating over any of these data structures.

    In terms of why the characterInfo is of length 256 when your text contained 600 characters, this is likely because you are querying these data structures before the text has been updated / processed which happens just before the camera is rendered. For instance, if you were to change the text in Awake and query these data structures, they would reflect the result of the previous text. Not sure this is the case for you but it is certainly one of the possibility.

    Are you changing the text in Awake or some update loop? Are you trying to access this information perhaps before that text has been rendered?

    One way to get around this is as follows: For times where you need to access these data structure before the text is rendered, you can force a text update by using the TextComponent.ForceMeshUpdate() function. This will force an immediate update of the text where these data structures will be populated. There are several posts on the forum about ForceMeshUpdate().

    Let me know if the above was the issue and if not, we'll keep digging.
     
  3. unity_zXeR0BHpYJM_Tw

    unity_zXeR0BHpYJM_Tw

    Joined:
    Mar 27, 2020
    Posts:
    5
    thank you very very much for the response.
    I guessed that it creates arrays with the size of pow 2. I can see
    wordInfo.Length
    of 16, 32, 64... but It doesn't work for characterInfo.Length it's always 256.

    I've tried
    tmp.ForceMeshUpdate()
    before getting data. but again.
    characterInfo.Length
    is 256 while the last word's
    wordInfo.lastCharacterIndex
    is 383.
     
    Last edited: Aug 5, 2022
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    When are you trying to access the content of the characterInfo? By when, I mean in Awake(), Start(), etc.

    Does the whole text display. Ie. all 600 characters?

    What version of the TMP package are you using?
     
  5. unity_zXeR0BHpYJM_Tw

    unity_zXeR0BHpYJM_Tw

    Joined:
    Mar 27, 2020
    Posts:
    5
    on click on textmeshpro.
    text updates in every frame. but the content remains intact. so the text and the number of words and characters are equal to the previous frame's.

    yes the whole text displays and
    TMP_TextUtilities.FindIntersectingWord()
    and
    lastWordInfo.lastCharacterIndex
    return the right indexes.

    Version 3.0.6

    what I need: when player clicks on a sentence of the content which contains paragraphs. I want to get the bounding box of the clicked sentence.
    so I need the TopLeft position of the first character, bottomRight of the last character, and the width of tmp itself.
    I've managed to target the sentence. I know the index of the first word and the size of the sentence. hence I know the first and last character indexes of the sentence.
    but the problem is: when I want to get character info in order to get topLeft and bottomRight vectors it throws an error because the index is out of range. ( > 256)!
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Do you get the same behavior with static text? Ie. test your implementation without changing the text.

    Seems like the issue might be the result of your code being executed before the textInfo has had a chance to get updated on that frame.
     
  7. unity_zXeR0BHpYJM_Tw

    unity_zXeR0BHpYJM_Tw

    Joined:
    Mar 27, 2020
    Posts:
    5
    Yes even without changing the text the problem is still there.
    the method runs after a click event. and at the beginning I've used
    tmp.ForceUpdateMesh();

    I added another script attached to TMPro and tried to get data with calling a coroutine from start() and it gives the right data even if text changes in every frame.
    but when I try to get it on click it won't work.
    it seems the problem is not the text change. it doesn't work on the frame that player clicks on the text mesh pro.
     
  8. unity_zXeR0BHpYJM_Tw

    unity_zXeR0BHpYJM_Tw

    Joined:
    Mar 27, 2020
    Posts:
    5
    any Ideas?
     
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Any chance you can provide me with a project / scene that would enable me to take a closer look?

    You can provide me with the above by exporting a scene along with relevant script and provide this via PM or provide me with some private link in a PM or submit a bug report with the project and steps.