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
3
Question by Grimmy · Oct 26, 2010 at 02:17 PM · meshtextscale

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 :)

Comment
Add comment
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

6 Replies

· Add your reply
  • Sort: 
avatar image
8

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.

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 Statement · Mar 27, 2011 at 01:08 PM 1
Share

Actually, I've gone through hell and beyond with bounds of text. It isn't accurate at all.

avatar image
1

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;
 }
Comment
Add comment · Show 2 · 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 aeroson · Sep 26, 2015 at 10:25 AM 0
Share

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;
 }
avatar image trzy · Aug 08, 2017 at 04:03 AM 0
Share

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!

avatar image
0

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.

Comment
Add comment · 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
0

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

Comment
Add comment · 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
0

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.

Comment
Add comment · Show 2 · 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 entity476 · Feb 27, 2015 at 11:32 PM 1
Share

Thinking back, the new UI has also all the features to do so, and I just noticed how old the thread is!:)

avatar image Taylor-Libonati · Feb 28, 2015 at 12:32 AM 0
Share

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.

  • 1
  • 2
  • ›

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

6 People are following this question.

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

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


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