Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question EditorGUI.helpbox height problem

Discussion in 'Immediate Mode GUI (IMGUI)' started by unity_tln4jrR0EJbLMg, Mar 15, 2024.

  1. unity_tln4jrR0EJbLMg

    unity_tln4jrR0EJbLMg

    Joined:
    Mar 15, 2021
    Posts:
    2
    I have a problem with calculating height of a helpbox (EditorGUI.helpBox). This style uses word wrapping so the height depends on two factors: amount of text and width of the window.

    I have made an example, so I can test this. Upper field is a text field and lower part is a helpbox that uses text entered in the upper field. This example illustrates several problems which occur when I shrink the width of the window. On the first image everything is OK, but as I decrease the width...

    a) notice how the text moves out of the box at the bottom
    b) if I shrink the window to the minimum width, text completly moves out from the box and overlaps with Add Component button.

    The last image show proper height calculating by Unity's PlatformEffector2D component so it can be done (and it works properly with all Unity components' helpboxes) but I don't know how.

    helpbox.png

    Here's the code of this example and some information. I use EditorGUI.helpBox. I cannot use EditorGUILayout.helpbox because I want the helpboxes to appear on lists, so I must use PropertyDrawers.

    Unfortunately the GetPropertyHeight method doesn't have a Rect position parameter so I must use EditorGUIUtility.currentViewWidth so I assume that is a part of the problem (i.e. height of the drawer does not match the height of the helpbox because the widths are different).

    But I have no idea from where can I get the position.width in the GetPropertyHeight method (and if this would help solve the problem).

    So my questions are:
    a) Are there any better ways to calculate proper height of the helpbox with word wrapping and dynamic width?
    b) How can I get proper width for the GetPropertyWidth method?

    Code (CSharp):
    1.  
    2. public class ExampleMB5 : MonoBehaviour
    3. {
    4.     public ExampleClass5 class1;
    5. }
    6.  
    7. [System.Serializable]
    8. public class ExampleClass5
    9. {
    10.     public string string1;
    11. }
    12.  
    13.  
    14. [CustomPropertyDrawer(typeof(ExampleClass5))]
    15. public class ExampleClass5_MI : PropertyDrawer
    16. {
    17.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    18.     {
    19.         Rect R = new Rect(0, 0, EditorGUIUtility.currentViewWidth, 150);
    20.  
    21.         GUI.skin.textArea.wordWrap = true;
    22.  
    23.         property.FindPropertyRelative("string1").stringValue = EditorGUI.TextArea(R, property.FindPropertyRelative("string1").stringValue, GUI.skin.textArea);
    24.  
    25.         GUIContent content = new GUIContent(property.FindPropertyRelative("string1").stringValue);
    26.  
    27.         float width = position.width;
    28.         float helpBoxHeight = EditorStyles.helpBox.CalcHeight(content, width);
    29.  
    30.         Rect HR = new Rect(0, 160, EditorGUIUtility.currentViewWidth, helpBoxHeight);
    31.         EditorGUI.HelpBox(HR, content.text, MessageType.Info);
    32.     }
    33.  
    34.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    35.     {
    36.         float height = 150;
    37.         height += 10;   // Vertical Space
    38.  
    39.         GUIContent content = new GUIContent(property.FindPropertyRelative("string1").stringValue);
    40.  
    41.  
    42.         float width = EditorGUIUtility.currentViewWidth; // <-------- There is no 'Rect position' parameter in GetPropertyHeight method, so I have to take width from EditorGUIUtility.currentViewWidth.
    43.  
    44.         float helpBoxHeight = EditorStyles.helpBox.CalcHeight(content, width);
    45.  
    46.         height += helpBoxHeight;
    47.  
    48.         return height;
    49.     }
    50.  
    51. }
     
  2. unity_tln4jrR0EJbLMg

    unity_tln4jrR0EJbLMg

    Joined:
    Mar 15, 2021
    Posts:
    2