Search Unity

Seriously... How does GUIStyle.CalcSize() work?

Discussion in 'Immediate Mode GUI (IMGUI)' started by pogoli, Jun 23, 2008.

  1. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    I am trying to implement a fancy tooltip system where a line of text fits nicely into a GUI/Layout.Window. It expand out horizontally until it runs out of text or reaches a predetermined limit, and then extend vertically until it runs out of text or reaches a predetermined height limit.

    I am trying to use CalcSize() to determine first if it will exceed this maximum width. That seems to work out alright. Then if it does exceed the max width I set the style's fixedWidth to my maximum width, and turn on word wrap and ask CalcSize() to report the new height. I expect it to return something that represents the height that the string will fill in that style (with the fixed width). I end up getting a number much larger than I expect for the height.

    I read the other forum entries about CalcSize and they recommend using GUILayout to solve the problem. I tried this as well, but when I passed in GUILayout.MaxHeight()... it did not actually cap the height of the window.

    Any thoughts, suggestions, solutions, or explanations would be most appreciated.

    ~Aaron
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    The function you are looking for is GUIStyle.CalcHeight. This takes the width as a parameter.
     
  3. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    Thanks, that worked great!

    What does CalcSize() return for height then when word wrap is on? Is it just not valid for this case?

    Also, could someone give a more detailed description of what CalcMinMaxWidth() does and what it might be used for?

    Thanks
     
  4. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    CalcSize does not take word wrap into account. So if you have no newlines, it will return the height on one line of text + the style's vertical padding.

    CalcMinMaxWidth is used by the layouting system to determine the min and max widths of a control.I honestly can't remember the details :)
     
  5. megan_l_fox

    megan_l_fox

    Joined:
    Jul 10, 2010
    Posts:
    35
    Sorry to bump an age-old thread here, but as far as I can tell in Unity 3.1.0f3, CalcSize does in fact pay attention to the wordWrap property. I have to set it to false to get a useful result, which invalidates the whole point of CalcSize - I don't exactly want to know what the wrapped text length would be for the minimum-width window. Quite the opposite, I'm usually checking to see what the width would be in worst-case, and then clamping it back.

    Thus, to get it to work presently, I have to set it to false before CalcSize, then true immediately after (before CalcHeight).

    Fixed in the current version, possibly?

    This is also for one of the custom styles in the array, if that matters.
     
  6. Yuriy

    Yuriy

    Joined:
    Feb 18, 2012
    Posts:
    1
    Sorry for bad English
    I need to place text on the width of GUI.Box and use hyphens
    Unity supports only Alignment Left Right and Center
    Please show me your code