- Home /
How to calculate a TextMesh width without rendering it ?
Hi all,
I am trying to create a wrapping function for TextMeshes. I want it to be based on a maximum width from the global world.
To do so I think I need to know how wide a letter or a word will be in the world. Right now I can get this by rendering it temporarily and use the bounds.size property of the renderer. For better performances, I would prefer to be able to calculate this width without rendering the text, knowing the various TextMesh attributes.
Someone wanted to use the planes generated by the TextMesh to do so, but it sounds to be impossible via this method. (Other thread regarding the TextMesh wrapping without answer.)
Does anybody have an idea ? Thanks !!!
Have you played around with GUIstyle.CalcSize(new GUIContent("text here"));
? I'd imagine there's some linear relationship between those pixel sizes and world sizes.
Yep I tried with the GUIText but I haven't found a relationship on which I can rely yet. I may go further on this solution...
I'm also quite sure there should be a linear relation between the size calculated for GUI and the resulting size of a textmesh since both use the font's size / kerning.
Just to be clear: We don't talk about the almost deprecated GUIText component. We talk about the UnityGUI class GUIStyle. Just create a style with your desired font / size and use GUIStyle.CalcSize to claculate the required size. Of course you have to find the scale between those pixel dimensions and world space width / height
Answer by FabienD · Feb 21, 2012 at 01:17 AM
I will answer myself, or kind of.. Thanks to the orientation from Owen Reynolds and Bunny83 I was able to establish the size factor linking the GUIContent and a TextMesh. It is safe to assume for a font-size greater than 10 that the factor is 0.1. Example : For a text rendered using a GUIContent which has a width of 507 pixels, the size of the same text rendered using a TextMesh with the same style will have a width (Bounds.size) of 50.7 units.
Using this factor, I have got now a 3D Text (TextMesh) wrapper which is working well !
A good thing to do will be to test this using various fonts...
Thanks very much for your help guys.
Answer by Anxo · Feb 17, 2012 at 06:15 PM
Use the same method you are using now, but instead, when you create the text assign it (via script or by prefab settings) to a layer that is ignored by the camera. If you at some point wanted to show the text, you can then change its layer.
Your answer
Follow this Question
Related Questions
Wrapping a Textmesh text 5 Answers
Adjusting the transform width to match wrapped text (TextMeshPro) 0 Answers
Wrap text textmesh 0 Answers