EditorGUILayout.PropertyField cannot draw custom class?

I have a class A that I marked as [Serializable]. In the class it contained 3 public string so if I declare that with public, in the inspector it would show up as 3 string under a drop down.

[System.Serializable]
public class A {

	public string englishText;
	public string thaiText;
	public string japaneseText;

}

However, I have one bigger class, B that contained many other things I have written custom inspector for. In this class B I also have some instance of public A in it. (Let’s say the name of this variable is aProp) I wanted to use A’s default inspector style so I think I could use

SerializedProperty aProp = info.FindPropertyRelative("aProp");

and then

 EditorGUILayout.PropertyField(aProp,GUIContent.none);

I think it would show up as a foldout, but no, only foldout arrow appeared and nothing would happen if I click on it except the arrow will flip pointing downward. Nothing got folded out. Why? Is the PropertyField limited to 1 line so foldout cannot display?

If you’re just wanting those properties to draw, include the includeChildren = true parameter in your EditorGUILayout.PropertyField call.

If you have a custom editor for class A this approach will not utilize it however.

EDIT: Karl’s post is definitely the right way to handle this. I’m leaving this post up just in case the code snippet is useful, but please upvote his answer!


I had this same issue with the same foldout behaviour, and I’m not sure why this doesn’t work, exactly.

I found a workaround thanks to vexe in this post: Unable to draw propertyfield in inspector for some classes, findProperty returns null. - Unity Answers

Here’s my code (notice in my case I don’t use FindPropertyRelative, since aProp is a member of my class):

var aProp = this.serializedObject.FindProperty("aProp");
int startingDepth = aProp.depth;
// Move into the first child of aProp
aProp.NextVisible(true);
do
{
    EditorGUILayout.PropertyField(aProp, true);
    aProp.NextVisible(false);
// Quit iterating when you are back at the original depth (you've drawn all children)
} while (aProp.depth > startingDepth);

If anyone knows a better way or at least knows why EditorGUILayout.PropertyLayout on the Serialized type with Serialized children doesn’t work, I’d still love to know.