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
1
Question by ZDS Alpha · Mar 29, 2014 at 04:27 PM · sizefonttextmesh

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.

Comment
Add comment · Show 2
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 LijuDeveloper · Apr 07, 2014 at 09:51 AM 1
Share

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;
  }
 }
avatar image ZDS Alpha LijuDeveloper · Apr 07, 2014 at 10:04 AM 0
Share

Font size is always zero.

I need a vector2 that gives height and width of text

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 
  1. https://docs.unity3d.com/Documentation/ScriptReference/Bounds.html

  2. https://docs.unity3d.com/Documentation/ScriptReference/Renderer-bounds.html

  3. http://docs.unity3d.com/Documentation/ScriptReference/Bounds-size.html

Comment
Add comment · Show 5 · 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 ZDS Alpha · Apr 07, 2014 at 12:30 PM 0
Share

Testing script....

avatar image ZDS Alpha · Apr 07, 2014 at 12:42 PM 0
Share

Thank youuuuuuuuuuuu VEeeeeeeeeeeery $$anonymous$$uch. Thanks for helping me I have solved my problem.

avatar image ZDS Alpha · Apr 07, 2014 at 12:47 PM 1
Share

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

avatar image LijuDeveloper · Apr 08, 2014 at 04:07 AM 0
Share

welcome ZDS Alpha

avatar image Wolfram · Sep 17, 2015 at 11:16 AM 0
Share

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.

avatar image
1

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.

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 ZDS Alpha · Apr 07, 2014 at 09:21 AM 0
Share

I dont understand what you mean?

Can you give me a function that returns size of text$$anonymous$$esh in Vector2.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Can I get color(or grey scale value) array for a font given a font size without using render texture? 0 Answers

iOS Font Size much smaller on iPad 4 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