Search Unity

TextMesh Pro CharacterInfo limit of 255?

Discussion in 'UGUI & TextMesh Pro' started by VarangianDonut, Dec 20, 2018.

  1. VarangianDonut

    VarangianDonut

    Joined:
    Dec 20, 2018
    Posts:
    3
    I've made a script for fading text onto the screen based on the workings of one I found on the forums.

    Code (CSharp):
    1. var materialIndex = textComponent.textInfo.characterInfo[charIndex].materialReferenceIndex;
    2.         var newVertexColors = textComponent.textInfo.meshInfo[materialIndex].colors32;
    3.         var vertexIndex = textComponent.textInfo.characterInfo[charIndex].vertexIndex;
    4.  
    5.         newVertexColors[vertexIndex + 0].a = (byte)alpha;
    6.         newVertexColors[vertexIndex + 1].a = (byte)alpha;
    7.         newVertexColors[vertexIndex + 2].a = (byte)alpha;
    8.         newVertexColors[vertexIndex + 3].a = (byte)alpha;
    I call
    textComponent.ForceMeshUpdate()
    before this and
    textComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32)
    afterwards. Sometimes when I print to the textbox, it works fine - but other times, however, I have an out of bounds error on
    characterInfo[charIndex]
    .

    After much investigation, I discovered this happens when charIndex is too high - it seems anything over 255 causes the error. How do I get around this? I need to control the alpha on all characters in the textbox, not just the first 255. Thanks for any help.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The CharacterInfo data structure / array is not limited to 255 characters. It is allocated in blocks to accommodate the number of characters the text object will contain.

    As you iterate over these characters makes sure you (skip non-visible characters) and only try to modified the vertex attributes of visible characters.
     
  3. VarangianDonut

    VarangianDonut

    Joined:
    Dec 20, 2018
    Posts:
    3
    I've just added the code to skip invisible characters now, but I'm still having the same issue unfortunately. IndexOutOfRangeException - index was outside the bounds of the array. Specifically when accessing characterInfo, when the index is above 255. Would it help if I posted the entire script? It's my first time working with TextMeshPro, so maybe I've made a mistake somewhere else.

    EDIT: Is there maybe a function I'm supposed to call beforehand? I add my string directly to textComponent.text, call ForceMeshUpdate, then when I try to modify each character's value (and the text is long enough), I get this issue. Did I miss a step?
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The full script would be useful but let's check another potential gotcha first.

    Since arrays like CharacterInfo[] are allocated in blocks, you cannot rely on their length when iterating over these as most of the time, they only contain partially valid data (ie. only valid data for the number of actual characters in this case). As such you have to rely on TextInfo.characterCount or whichever / related count.
     
  5. VarangianDonut

    VarangianDonut

    Joined:
    Dec 20, 2018
    Posts:
    3
    Thank you for your help, using TextInfo.characterCount has fixed the problem.
     
    Stephan_B likes this.