Custom Inspector: Mixing custom drawer and default property drawers

I’ve been playing around with ScriptableObjects and using them to define parameters for my Dialog display gameobject.  In previous instances, I’d just use constant strings and pass them to the function that creates and displays the dialog.  After reading about ScriptableObjects, I decided to give them a shot to build strings for the dialogs.

Everything was great, except for the inspector displaying long strings.  They just get truncated of the right side of the display.  The behavior exhibited by the NGUI UILabel inspector is much better, as any text to long to be displayed in the inspector is wrapped and the field is extended vertically.

I found the NGUI code in UILabelInspector that handles the displaying of strings across multiple lines, and took a copy to use in my own inspector.  Then I realized, with a custom inspector I have to display all the properties myself, and I didn’t want to (a) write a custom display for each property type, and (b) remember to extend the inspector anytime I modified my ScriptableObject class.

The second problem was solved by iterating over the SerializedProperties contained in the SerializedObject, while the first problem was solved by calling EditorGui.PropertyField on SerializedProperties I didn’t want to display myself.

I then added an exception for the ‘Script’ property, to make it non-editable.

This resulted in the code below:

public override void OnInspectorGUI()
{
	serializedObject.Update();
	SerializedProperty sp = serializedObject.GetIterator();
	if (!sp.NextVisible(true))
		sp = null;
	while (sp!=null)
	{
		if (sp.type == "string")
			DrawMultiLineString(sp.name);
		else
		{
			Rect position = EditorGUILayout.GetControlRect(GUILayout.Height(EditorGUI.GetPropertyHeight(sp)));
			if (sp.name == "m_Script")
			{
				EditorGUI.BeginDisabledGroup(true);
				EditorGUI.PropertyField(position, sp, new GUIContent(sp.displayName), true);
				EditorGUI.EndDisabledGroup();
			}
			else
				EditorGUI.PropertyField(position, sp, new GUIContent(sp.displayName), true);
		}
		if (!sp.NextVisible(true))
			sp = null;
	}
	serializedObject.ApplyModifiedProperties();
}

The call to DrawMultiLineString uses code directly from NGUI’s UILabelInspector, and is not available unless you have NGUI for Unity.

Leave a Reply