Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Roland1234 · Mar 12, 2013 at 05:18 AM · sizerenderertextmeshbounds

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.

alt text

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?

textmesh.jpg (151.9 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 12, 2013 at 05:57 AM 0
Share

Ideas to explore rather than a known solutions: you might take the $$anonymous$$esh.bounds and move it into global space and compare it to the one the renderer uses. It is possible the $$anonymous$$esh.bounds is more accurate. Another idea is to produce your own bounding box by evaluating the vertices. It is also possible the problem is unused vertices that you could prune from the mesh. I could see the Text$$anonymous$$esh code keeping a set of start vertices to use for the next character that never got used.

avatar image Roland1234 · Mar 13, 2013 at 06:01 AM 0
Share

From what I can see and have read it seems that there is no way to get the mesh that makes up the Text$$anonymous$$esh, so mesh.bounds and looking at mesh vertices is out. Thanks for the reply, though.

avatar image robertbu · Mar 13, 2013 at 06:39 AM 0
Share

Strange. I learned something. I wonder why they did not expose the mesh.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by AlkisFortuneFish · Feb 04, 2014 at 07:07 PM

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Roland1234 · Feb 06, 2014 at 05:44 PM 0
Share

Hi Alkis - it has been some time since I've used Text$$anonymous$$esh, I've pretty much abandoned trying to find the answer I was looking for and moved on to other text solutions since. I appreciate your looking into it, but am a bit confused regarding your answer. I was looking for a way to get a bounding box that better represented the actual mesh of the Text$$anonymous$$esh, but it sounds like you're trying to reconstruct the way Unity calculates the bounding box - which is what gives flawed/confusing results. If you could arrive at a method that would result in a center, width, and height for a box that's more accurately wrapped around the Text$$anonymous$$esh quads then I'd consider this question answered.

Either way, I tried testing your approach to try and verify if the bounding box height is at least calculated the way you've outlined, but am not getting anywhere near the correct results (maybe I misunderstood something). If you'd like I could join you in the forums to discuss the details, just create a thread and post a link if you're interested (to keep this page more on-topic with the question). Cheers!

avatar image
0

Answer by trul · May 17, 2015 at 08:07 PM

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);
     }

alt text

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


textmeshbounds.png (33.6 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jonher · Aug 22, 2017 at 02:31 PM 0
Share

Thx! This helped me to place a backgroud behind a Text$$anonymous$$esh by code..

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set RectTransform size relative to the size of the textbox? 1 Answer

How to keep game object aligned with text mesh 1 Answer

Placing a GameObject on top of another GameObject. 1 Answer

Mesh Renderer Matereal Aray reduce? 0 Answers

Matching an object's size 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges