Before updating to Windows 10 you may need to return your Pro license. More information here

guistyle.CalcSize returning unexpected values...

as the title says the vector2 im getting back is way off. it should be something around (100, 30)

 Vector2 size = skin.label.CalcSize(new GUIContent("Save Position"));

(14.0, 214.0) UnityEngine.Debug:Log(Object) is what im getting in the console...

any ideas whats going wrong?

more ▼

asked Jan 31, 2013 at 10:39 AM

avatar image

315 36 48 79

skin is my own GUISkin object and the label style uses standard arial at the standard size and the top and bottom padding are both 3

Jan 31, 2013 at 10:48 AM gheeler
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The problem is that CalcSize doesn't work correctly whenever word wrapping is enabled. The doc already states that "This function does not take wordwrapping into account". What it fails to mention is that it returns completely wrong values when word wrapping is on. Now, since wrapping is on by default in some skins, you need to turn it off explicitly in order to use CalcSize. For example, to center some text on the screen:

     GUIStyle style = GUI.skin.box;
     style.wordWrap = false;
     GUIContent content = new GUIContent("Your text here");
     Vector2 size = style.CalcSize(content);
     GUI.Box(new Rect((Screen.width - size.x) / 2, (Screen.height - size.y) / 2,
       size.x, size.y), content, style);

Alternately, you could enable word wrapping, define yourself the width your text should have, then use CalcHeight to get the corresponding height:

     GUIStyle style = GUI.skin.box;
     style.wordWrap = true;
     GUIContent content = new GUIContent
       ("Your very long text here that needs to get word wrapped");

     float height = style.CalcHeight(content, 500);
     GUI.Box(new Rect((Screen.width - 500) / 2, (Screen.height - height) / 2,
       500, height), content, style);
more ▼

answered May 10, 2014 at 09:41 AM

avatar image


(comments are locked)
10|3000 characters needed characters left

I am getting the same problem. The CalcSize is giving me (14,0 , 24.0) as a Vector2 return when my block of text easily fills a 730.0 wide and 400.0 tall box on the screen.

Edit: I could not find the solution for this

I have a GuiContent element with text.

CalcSize now returns a Vector2 (2400.0, 24.0)

CalcHeight, width 700, called on that same GuiContent should return 96 (4x 24)

CalcHeight however always returns 24.0 regardless of the width. Not sure why.

I ended up just using CalcSize and calculating my own height from that.

more ▼

answered Oct 19, 2013 at 12:30 PM

avatar image

56 2 2 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jan 31, 2013 at 10:39 AM

Seen: 1500 times

Last Updated: May 10, 2014 at 09:41 AM