Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by rjr · Aug 05, 2016 at 12:17 PM · uitext

Get The size of the Text in unity UI

Hello i am using unity 5.4
I have a image having a child Text. i want to scale the image based on the string size given to the text. i am using following code to get the work done.

 m_textBgImage = Instantiate (m_textBgPrefabs);    // This is a image prefab has text as its child 
 m_chatText = m_textBgImage.GetComponentInChildren<Text> ();
 
 m_chatText.text = m_textToDisplay;
 Vector2 widthHeight = GetSizeOfWord (m_textToDisplay);
 textBgImage.GetComponent<RectTransform>().sizeDelta = widthHeight;
 
 
 
 Vector2 GetSizeOfWord(string word)
     {
         float width = 0.0f;
         float biggestWidth = 1.0f;
         float biggestHeight = 0.0f;
         int spaceCounts = 0;
 
 
         print ("m_chatText.fontSize : "+m_chatText.fontSize); 
         foreach (char c in word)
         {
             CharacterInfo charInfo;
             if (c == ' ') 
             {
                 spaceCounts++;
             }
             else 
             {
                 m_chatText.font.GetCharacterInfo (c, out charInfo, m_chatText.fontSize);
                 width += charInfo.glyphWidth;
 
                 if (biggestWidth < charInfo.glyphWidth) {
                     biggestWidth = charInfo.glyphWidth;
                 }
                 if (biggestHeight < charInfo.glyphHeight) {
                     biggestHeight = charInfo.glyphHeight;
                 }
                 print ("Char : "+c);
                 print ("Advance : "+charInfo.advance);
                 print ("Height : "+charInfo.glyphHeight);
                 print ("Width : "+charInfo.glyphWidth);
                 print ("Index : "+charInfo.index);
                 print ("Bearing : "+charInfo.bearing);
                 print ("MaxX : "+charInfo.maxX);
                 print ("MaxY : "+charInfo.maxY);
                 print ("Size : "+charInfo.size);
                 print ("Style : "+charInfo.style);
                 print ("uvBottomLeft : "+charInfo.uvBottomLeft);
                 print ("uvBottomRight : "+charInfo.uvBottomRight);
                 print ("uvTopLeft : "+charInfo.uvTopLeft);
                 print ("uvTopRight : "+charInfo.uvTopRight);
             }
         }
         print ("Space count : "+spaceCounts);
         float textWidth = width + (biggestWidth * spaceCounts);
         float textHeight = Mathf.Floor (textWidth / m_maxLengthOfText);
         textWidth = Mathf.Min( m_maxLengthOfText,width);
         print ("W : " + textWidth + " H : " + textHeight * biggestHeight);
         return new Vector2(textWidth, textHeight * biggestHeight);
     }



The "out charInfo" is most of the time outputting zero value and all the print statements displays zero, and randomly once in a blue moon it gets the correct value, the height and width of the character.
Does anyone know why i am getting zero values , where i am missing out?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoJQB9 · Aug 05, 2016 at 02:04 PM

I guess you mean the font size?

For UnityEngine.UI.Text objects you can get info of the displayed text with the cachedTextGenerator and cachedTextGeneratorForLayout properties.

https://docs.unity3d.com/ScriptReference/TextGenerator.html

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 rjr · Aug 08, 2016 at 05:04 AM 0
Share

Hey Diego , Thanks for your time, Unfortunately Text generator is not what i am looking for, and also i meant length of the string in pixels in canvas space not the font size. I had to tweak the above code to get it work correctly

avatar image
0

Answer by rjr · Aug 08, 2016 at 05:17 AM

Solved!!!

I had kept m_textBgImage prefab width and height to zero in the inspector, I changed it to width =1 and Height = 1

And after instantiating m_textBgImage i gave a small delay before calling GetSizeOfWord(string word) ie. i put GetSizeOfWord(string word) in a co-routine and waited for 0.1 seconds. and it seems to be calculating proper pixel values for each characters.

I suppose after instantiation unity takes some time to initialise the Text property such as its font, material info which is accessed in GetCharacterInfo method, So if it is accessed before initialisation it will return zeros

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

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

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

Related Questions

Selecting a part of string in input field 1 Answer

Why Null Reference Exception here? 3 Answers

How to supersample TTF text of UI in 3D graphics scene? 0 Answers

How to apply effects to UI Text in Unity 4.6 1 Answer

How do i position this text to go where i want? 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