- Home /
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? (without having to build the gui for each serializable class myself)
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();
}
}
I also thought it should work, but it appear not to work with EditorGUILayout for some reason. The layout is correct. When you expand it the members aren't drawn, but they take up space.
Answer by casimps1 · Apr 20, 2012 at 03:17 PM
Yes, you can make this work. You are just missing the includeChildren param from your PropertyField calls. Try this:
EditorGUILayout.PropertyField(prop,true);
Your answer
Follow this Question
Related Questions
How to expose a struct type variable from dll, which is not declared [Serializable] 1 Answer
Behaviour of custom inspector on Undo 1 Answer
EditorGUI.Slider positioning 1 Answer
Can i add GUI below the Play Button, or Move the Scene Gizmo? 0 Answers
Custom inspector editor - how to put new editor fields in a specific place 1 Answer