Is it possible to find the width of a text mesh?

In pixels or localSCale or sanything really as I want to base the relative position of other objects at the end of the text line.

ie..where the smiley face is my 'other object' the smiley needs to be positioned differnently depending on the length of the textmesh...

shortword :)

veryveryveryveryverylongword :)

Well, just an idea here -- if your text is always aligned with some axis, you can just use the renderer's bounding boxes -- something like this might work if your text runs along the X-axis:

using UnityEngine;
using System.Collections;

public class SmileyFace : MonoBehaviour 
{
    public TextMesh MyTextMesh;
    void Start() 
    {
        Bounds textBounds = MyTextMesh.renderer.bounds;
        transform.position = new Vector3(textBounds.max.x, textBounds.center.y, textBounds.center.z);
    }
}

Hope that compiles -- this component would be on your smiley face, and you would have to set MyTextMesh to your text mesh before using, obviously. This should work if you're using 3D text for stuff like UI or the text is axis-aligned and not rotating.

But if your text is not always aligned with any of the axis, you can't accurately use the bounding boxes -- TextMesh does not have local bounds (like the MeshFilter.mesh), so I don't know how to overcome that without adding extra variables to keep track of the text rotation/scale etc.

My function - find TextMesh text width without renderer.bounds.x

public static float GetWidht(TextMesh mesh)
{
	float width = 0;
	foreach( char symbol in mesh.text)
	{
		CharacterInfo info;
		if (mesh.font.GetCharacterInfo(symbol, out info))
		{
			width += info.width;
		}
	}
	return width * mesh.characterSize * 0.1f * mesh.transform.lossyScale.x;
}

When you are using GUI text, it won't be possible a colleague of mine tried to put images in a text, and ended up just creating the whole text as an image. Obviously, this might not work in your case, if the text is set dynamically.

Found this, thought it might help someone:

DF GUI plug in would do that if you don’t mind to get involved with gui controls. If embedding a sprite to a label is what you need, here is the first link I found, but probably there is an example in the package as well. Moreover, DFGUI is now open and you can find its classes in Github.

Here’s an improved solution which gets the actual rendered width of the TextMesh even if it contains newlines and adjusts to font size, style & scale changes. It’s based on the answer by @Mefistofel and the improvements made by @aeroson

public MeshFilter containerMeshFilter;
public TextMesh textboxTextMesh;
    
[ContextMenu("Debug Log Widths")]
public void EditorDebugWidth() {
    Debug.Log("The container width is " + GetContainerMeshWidth(containerMeshFilter));
    Debug.Log("The text width is " + GetTextMeshWidth(textboxTextMesh));
}

private float GetContainerMeshWidth(MeshFilter meshFilter) {
    Bounds meshBounds = meshFilter.mesh.bounds;
        
    return Mathf.Abs(meshBounds.max.x - meshBounds.min.x) * Mathf.Abs(meshFilter.transform.lossyScale.x);
}
    
private float GetTextMeshWidth(TextMesh textMesh) {
    string[] textLines = textMesh.text.Split('

');
int widestLineWidth = 0;

    // Iterate through each line of text
    foreach (string textLine in textLines) {
        int width = 0;
            
        // Iterate through each symbol in the current text line
        foreach (char symbol in textLine) {
            if (textMesh.font.GetCharacterInfo(symbol, out CharacterInfo charInfo, textMesh.fontSize, textMesh.fontStyle))
                width += charInfo.advance;
        }

        if (widestLineWidth <= 0 || width > widestLineWidth)
            widestLineWidth = width;
    }

    // Multiplied by 0.1 to make the size of this match the bounds size of meshes (which is 10x larger)
    return widestLineWidth * textMesh.characterSize * Mathf.Abs(textMesh.transform.lossyScale.x) * 0.1f;
}