TextMesh.renderer.bounds.extents not 100% accurate?

I’ve been looking for a better way of getting the size of a TextMesh object, but every answer I’ve found relies on using it’s renderer.bounds.center/extents properties. This works in general, but fails to give as precise results as other object’s bounds properties.

I was expecting that the bounds box would perfectly wrap around the quads that make up the TextMesh, but as you can see that is not the case. Also, I’ve noticed that modifying the LineSpacing property will change the box in unpredictable ways.

I’d hate to find another text solution because it otherwise works like I’d expect it to. Has anybody else noticed this and found a way to get accurate results or am I not understanding something about TextMesh?

I know it’s been a VERY long while since this question was asked, but I think I’ve just found the answer, after a two hour headache of fonts disappearing when partially off screen and vertical anchor settings being ignored…

The height of the renderer bounding box is set by the line spacing of the .fontsettings asset multiplied by the TextMesh character size and line spacing.

The TextMesh line spacing assumes space under each character, so if it is set to 1, your bounds will be twice the font line height, even if you only have one line of text.

Bounds height = 2*(Number of Lines)(Asset Line Spacing)(TextMesh Line Spacing)*(TextMesh Character Size)

Edit: That headache was worse than I thought… This should be:

The TextMesh line spacing assumes space under each character, so if it is set to 2, your bounds will be twice the font line height, even if you only have one line of text. If your asset’s line spacing is wrong, it’ll all be wrong.

Bounds height = (Number of Lines)(Asset Line Spacing)(TextMesh Line Spacing)*(TextMesh Character Size)

The asset line height should automatically be calculated for font assets generated by the TrueTypeFontImporter, while for custom fonts it should manually be set.

If you have a custom font, draw the renderer bounds, make sure that the text mesh line spacing is set to 1 and modify the font asset’s line spacing until the bounding box perfectly covers the tallest letter in your font.

Oh, also, to get the asset line spacing for a font imported using the TrueTypeFontImporter, since it does not publicly expose this, or in fact to set it via script, you can grab it using serialized properties, as per here.

SerializedObject so = new SerializedObject(customFont);
Debug.Log(so.FindProperty("m_LineSpacing").floatValue);

Hey, it looks like they have fixed this issue (in 4.6.4f1).
The bound box is calculated good enough.
The green filled rectangle is visualization for the calculated bounds.
This is how it is calculated:

public void UpdateSize(Bounds bounds)
	{
		transform.position = bounds.center;
		SpriteRenderer sr = GetComponent<SpriteRenderer>();
		float sx = bounds.extents.x/sr.bounds.extents.x;
		float sy = bounds.extents.y/sr.bounds.extents.y;
		transform.localScale = new Vector3(transform.localScale.x*sx, transform.localScale.y*sy,1f);
	}

46506-textmeshbounds.png

BTW, are there better solution for displayed text in unity than TextMesh?
UI text is not flexible enough for my purposes.