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
1
Question by benjaminoutram · Aug 07, 2017 at 09:48 AM · uitextsizeworldspacefontsize

How do I calculate how big text is in a world space canvas UI Text?

I have a Canvas (Render Mode = "World Space", and its Rect Transform has Width = 4, Height = 2) with a Canvas Scaler with "Dynamic Pixels Per Unit" = 20. I have placed a UI Text in the canvas, and would like to know how large in absolute terms the text is in the virtual space. The text is set to Font Size = 12.

How should I calculate the absolute size of the text?

I am not sure I understand the relationship between the dynamic pixels per unit, the font size, the canvas size, and the absolute size of the text. Any help would be greatly appreciated.

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
-1

Answer by cardcastleusa · Mar 20, 2018 at 04:26 PM

@benjaminoutram The Following Code adjust the height of the text GameObject only! If you need to adjust the parent you will add the heightChange to the parents RectTransform too.

 public class Example : MonoBehaviour {
 
 public GameObject text;
 public float wordWrapSpacing;
 
 public void AdjustSize()
 {
 GUIStyle gui = new GUIStyle();
 RectTransform textRect = text.getComponent<RectTransform>();
 
 //This calculates the size needed to hold all of the text based on Font Size
 Vector2 adjustmentSize = gui.CalcSize(text.getComponent<Text>().text);
 
 //Adjust the wordWrapSpacing based on the font you are using.
 float heightChange = size.y * (size.x / textRect.rect.width) + wordWrapSpacing * (size.x / textRect.rect.width);
 
 //This is needed because your text might not have to be adjusted.
 if(textRect.rect.height < heightChange)
 {
 textRect.sizeDelta = new Vector2(textRect.rect.width, textRect.rect.height + heightChange);
 }
 }//End Function
 
 }//end Class
 
 //Note If you are messing with both height and width you will need to multiply the wordWrapSpacing by the percentage change that has occurred width wise.
 
 

This is a very manual way of doing this and unity has many built in functions.

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 shieldgenerator7 · Jan 04, 2019 at 02:53 AM 0
Share

Text.text is not a GUIContent and thus can't be passed into GUIStyle.CalcSize(), which makes this whole thing not work.

avatar image
0

Answer by shieldgenerator7 · Jan 04, 2019 at 06:56 AM

I know it's not exact, but this works for me for finding a rough size of GUI text:

 static float getTextWidth(Canvas canvas, Text text, int length)
 {
      return text.fontSize * 0.5f * length * canvas.transform.localScale.x;
 }
 static float getTextHeight(Canvas canvas, Text text)
 {
      return text.fontSize * canvas.transform.localScale.y;
 }
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

112 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 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

Moving 3D Text 1 Answer

UI Relative to screen size 1 Answer

How to make parent UI element change size to fit children? 0 Answers

UI Text: Wrap and Expand to keep aspect ratio 2 Answers

How to get the current "best fit" size of a Text component? 3 Answers


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