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"));
Debug.Log(size);

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

any ideas whats going wrong?

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.

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);