• 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
14
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Styling an individual button in SelectionGrid 0 Answers

Create GUI Texture using GUI Layout 2 Answers

GUI.Label top margin problem (without layout) 0 Answers

Method is called, but GUI doesn't show up 1 Answer

Changing Font Size 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges