- Home /
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 :)
Answer by VS48 · Oct 26, 2010 at 07:21 PM
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.
Actually, I've gone through hell and beyond with bounds of text. It isn't accurate at all.
Answer by Mefistofel · May 06, 2015 at 06:44 AM
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;
}
Thank you, but you didn't account for font size and style. Here have my version.
public void SetText(string text)
{
foreach (var t in texts)
{
t.text = text;
}
if (background && texts.Length>0)
{
var s = background.transform.localScale;
s.x = GetWidth(texts[0]);
background.transform.localScale = s;
}
}
public static float GetWidth(Text$$anonymous$$esh mesh)
{
float width = 0;
foreach (char symbol in mesh.text)
{
CharacterInfo info;
if (mesh.font.GetCharacterInfo(symbol, out info, mesh.fontSize, mesh.fontStyle))
{
width += info.advance;
}
}
return width * mesh.characterSize * 0.1f;
}
Can you explain what the factor of 0.1 is for? I can see that it yields the correct result in my own testing but I can't find an explanation of what this is in the Unity scripting docs. Thanks!
Answer by xeophin · Oct 26, 2010 at 07:46 PM
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.
Answer by Taylor-Libonati · Feb 27, 2015 at 10:49 PM
Found this, thought it might help someone: https://github.com/Tamulur/AskSocrates/blob/master/Assets/Scripts/Utilities/TextSize.cs
Answer by entity476 · Feb 27, 2015 at 11:22 PM
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.
Thinking back, the new UI has also all the features to do so, and I just noticed how old the thread is!:)
haha yeah, I just came across this thread today having a similar problem. I forgot that Unity has released an updated UI system, I will have to check it out.
Your answer
Follow this Question
Related Questions
How to replace this Gui text with text mesh please ? 1 Answer
Can I Duplicate The Default Cube Mesh To Give It Different Scale Factors? 0 Answers
Text Mesh - Ignores Backface culling & Depth Test? 2 Answers
Shuriken mesh size over lifetime question 0 Answers
Mesh Collider from 3d max animation? 1 Answer