IndexOutOfBoundsException when using <u> or <mark> with vertices count bigger than 65535

Creating a TextMeshPro with enough text together with underlines () will eventually cause an IndexOutOfBoundsException in the file TMP_Text.cs in the method DrawUnderlineMesh at line 5750, because vertices[index] is accessed while index > m_textInfo.meshInfo[underlineMaterialIndex].vertices.Length.
This causes an exception in the log, as well as the underlines and/or highlights not to appear in the TextMeshPro.

This only happens if the vertices array has a length of 65532 before new meshes are added. In this case, the ResizeMeshInfo method in file TMP_MeshInfo.cs, which gets called to allocate a larger buffer, sets the target size to Mathf.Min(size, 16383). There’s some commented out code above this:

// If the requested size will exceed the 16 bit mesh limit, switch mesh to use 32 bit.
//if (size > 16383 && this.mesh.indexFormat == IndexFormat.UInt16)
//    this.mesh.indexFormat = IndexFormat.UInt32;

Here’s a screenshot of the exception when too many tags are used:


The exception thrown here comes from the method DrawTextHighlight and follows the same pattern as described above, i.e. the vertices array has length 65532, the same ResizeMeshInfo method is called, then the IndexOutOfBoundsException in the screenshot occurs.

This is indeed a known limitation / issue which I need to address at some point.

Until then, I would recommend trying to split up the text object in multiple smaller ones. This would also be more efficient as most of the time a lot of the characters in this large object are not visible.

1 Like