Should EditorGUILayout.PropertyField work with Serializable classes?

I’m trying to create a custom inspector but I when try to display a Serializable class it doesn’t show up.
It does show the serializable class name as a label with the small foldable arrow next to it but when I try to unfold it, its empty.

At this point I am only trying to replicate the behaviour of the Unity editor, nothing fancy yet.
What do I do wrong? How do I get the current behaviour of the editor with serializable classes?

The MonoBehaviour for which I am building a custom inspector

public class SomeComponent : MonoBehaviour
{
    public ChangeColor SomeColor;
    public ChangeScore SomeScore;
}

The serializable classes

[System.Serializable]
public class ChangeColor 
{
    public Material Mat;
    public Color To;
}

[System.Serializable]
public class ChangeScore 
{
    public int Points;
    public Transform Position;
}

The custom inspector

[CustomEditor(typeof(SomeComponent))]
public class SomeComponentEditor : Editor
{
    private SerializedObject m_object;

    public void OnEnable()
    {
        m_object = new SerializedObject(target);
    }

    public override void OnInspectorGUI()
    {
        m_object.Update();

        GUILayout.Label("Some label", EditorStyles.boldLabel);

        var prop = m_object.FindProperty("SomeColor");
        EditorGUILayout.PropertyField(prop);

        prop = m_object.FindProperty("SomeScore");
        EditorGUILayout.PropertyField(prop);

        m_object.ApplyModifiedProperties();
    }
}

Hi,

When we draw properties we don’t recursively draw nested objects, just the top level object (this might be something we could add in the future :wink: ).

In 3.5 you can do this to recursively draw:

		GUILayout.Label("Some label", EditorStyles.boldLabel);

		var prop = m_object.FindProperty("SomeColor");
		EditorGUILayout.PropertyField(prop);
		
		if (prop.isExpanded)
		{
			foreach (SerializedProperty p in prop)
				EditorGUILayout.PropertyField (p);
		}
		
		prop = m_object.FindProperty("SomeScore");
		EditorGUILayout.PropertyField(prop);
		
		if (prop.isExpanded)
		{
			foreach (SerializedProperty p in prop)
				EditorGUILayout.PropertyField (p);
		}

In 3.4 you can use the prop.next and paths to figure it out. But 3.5 is much nicer in this regard.

1 Like