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.
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?
public class ExampleMB5 : MonoBehaviour
{
public ExampleClass5 class1;
}
[System.Serializable]
public class ExampleClass5
{
public string string1;
}
[CustomPropertyDrawer(typeof(ExampleClass5))]
public class ExampleClass5_MI : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Rect R = new Rect(0, 0, EditorGUIUtility.currentViewWidth, 150);
GUI.skin.textArea.wordWrap = true;
property.FindPropertyRelative("string1").stringValue = EditorGUI.TextArea(R, property.FindPropertyRelative("string1").stringValue, GUI.skin.textArea);
GUIContent content = new GUIContent(property.FindPropertyRelative("string1").stringValue);
float width = position.width;
float helpBoxHeight = EditorStyles.helpBox.CalcHeight(content, width);
Rect HR = new Rect(0, 160, EditorGUIUtility.currentViewWidth, helpBoxHeight);
EditorGUI.HelpBox(HR, content.text, MessageType.Info);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float height = 150;
height += 10; // Vertical Space
GUIContent content = new GUIContent(property.FindPropertyRelative("string1").stringValue);
float width = EditorGUIUtility.currentViewWidth; // <-------- There is no 'Rect position' parameter in GetPropertyHeight method, so I have to take width from EditorGUIUtility.currentViewWidth.
float helpBoxHeight = EditorStyles.helpBox.CalcHeight(content, width);
height += helpBoxHeight;
return height;
}
}