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
5
Question by x4000 · Mar 19, 2010 at 01:48 AM · guifontguilayout

Is there a way to measure the pixel with/height of a string with a given font?

I see the GUI Layout stuff for automatically laying out UIs in certain ways. And that is pretty cool overall. However, it is also really helpful to be able to precisely center various arbitrary strings (arbitrary thanks to localization or other factors, which has been an interesting challenge for me in past projects). With both DirectX and GDI, there is essentially a MeasureString method that you can pass a font, some text, and a max width to, and it gives back a Size property saying how wide/tall that text will be given the parameters you passed in. Surely there's something similar in Unity?

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
15
Best Answer

Answer by runevision · Mar 19, 2010 at 10:10 AM

You can use GUIStyle.CalcSize() which takes a GUIContent as parameter:

Vector2 sizeOfLabel = myStyle.CalcSize(new GUIContent("My string"));

You can use this on a GUIStyle you have made that uses the font of your choice.

GUILayoutUtility.GetRect() reserves space for the rect it returns, so it may influence your GUI layout. If you want a function that does not have side effects, CalcSize is the way to go.

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 x4000 · Mar 19, 2010 at 03:10 PM 0
Share

Thanks very much!

avatar image Jean-Fabre · Oct 21, 2010 at 10:52 AM 0
Share

yes, very helpfull answer thanks

avatar image
2

Answer by Eric5h5 · Mar 19, 2010 at 03:54 AM

Yes, you can use GUIUtility.GetRect(), where you pass in GUIContent and GUIStyle and get back a Rect. (Plus various overloads.)

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

No one has followed this question yet.

Related Questions

Change Font Style OnMouseEnter 0 Answers

GUI text font change 3 Answers

Help with GUILayout and tooltip(C#) 1 Answer

Cant change color of 3dText/Text Mesh 3 Answers

Is there a method for getting Scrollview to work in a GUILayout Window? 2 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