- Home /
Find textMesh size in unity in Vector2
How to find the size of font in unity. I mean find size of "Text Mesh" in unity.
I need a function that returns the size of textMesh in Vector2.
try this code
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : $$anonymous$$onoBehaviour
{
public Text$$anonymous$$esh textmesh;
void Start ()
{
int size =SizeOfText$$anonymous$$esh("New Text");// "New Text" is name of textmesh
print ("Size ="+size );
}
int SizeOfText$$anonymous$$esh(string text$$anonymous$$eshName)
{
textmesh = GameObject.Find (text$$anonymous$$eshName).GetComponent<Text$$anonymous$$esh>();
return textmesh.fontSize;
}
}
Font size is always zero.
I need a vector2 that gives height and width of text
Answer by LijuDeveloper · Apr 07, 2014 at 11:55 AM
I edit this code , try this one
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public TextMesh textmesh;
void Start ()
{
Bounds bound = BoundOfTextMesh("New Text");
//bound.size.x give total length and bound.size.y gives height of TextMesh (Vector 2)
print ("Size X = "+bound.size.x +"Size Y = "+bound.size.y);// Size of X and Y or Vector2
//print ("Left point x = "+bound.min.x +"Right point x = "+bound.max.x);
//print ("Bottom point Y = "+bound.min.y +"Top point Y = "+bound.max.y);
}
Bounds BoundOfTextMesh(string textMeshName)
{
textmesh = GameObject.Find (textMeshName).GetComponent<TextMesh>();
return textmesh.renderer.bounds ;
}
}
Note :
*Bounds is the axis-aligned bounding box fully enclosing the object in world space. Please check out
Thank youuuuuuuuuuuu VEeeeeeeeeeeery $$anonymous$$uch. Thanks for helping me I have solved my problem.
This is code which was needed. Now the size do not depends on the rotation.
Vector2 Text$$anonymous$$eshArea(Text$$anonymous$$esh textmesh){
Quaternion rotation = textmesh.gameObject.transform.rotation;
textmesh.gameObject.transform.rotation = new Quaternion ();
Vector2 ret = new Vector2 (textmesh.renderer.bounds.size.x, textmesh.renderer.bounds.size.y);
textmesh.gameObject.transform.rotation = rotation;
return ret;
}
Thanks for helping
Note that this answer is wrong - or to be precise, it only gives you the correct value if your transform is neither scaled or rotated. As per documentation of "Renderer.bounds":
"This is the axis-aligned bounding box fully enclosing the object in world space."
ZDSAlpha's script in the comments is a hack that temporarily removes any rotation from your object, but it still doesn't account for scaling, and may even fail if one of your scaling axes is 0.
So handle this answer with care - if your object is neither scaled, nor rotated, you can use it. Otherwise look for other workarounds here on the Answers site. There is no trivial way.
Answer by ava4414 · Mar 29, 2014 at 05:24 PM
this could help you:
http://docs.unity3d.com/Documentation/Components/class-TextMesh.html
http://docs.unity3d.com/Documentation/ScriptReference/TextMesh.html
if you are using a dynamic font you can find size with fontSize, but if you are only using a simple one, you can set its size with characterSize.
I dont understand what you mean?
Can you give me a function that returns size of text$$anonymous$$esh in Vector2.
Your answer
Follow this Question
Related Questions
Change the size of a custom font in a Canvas text 1 Answer
Changing Button/Label text size? 2 Answers
Changing the size of a GUI label 1 Answer
iOS Font Size much smaller on iPad 4 1 Answer