- Home /
How to handle Serializable classes in a CustomEditor?
Hi!
I'm trying to create a custom inspector for this class:
public class Section : MonoBehaviour {
public Lane[] lanes;
[System.Serializable]
public class Lane {
public Type type;
public float position;
public Path path;
public enum Type
{
Static,
Path
}
}
}
What I would like to achieve is showing either position or path properties for each element in the lanes array in the inspector, based on the selection of the type field (Static type will display position in inspector, Path will display path).
I created a custom inspector, but I don't know how to iterate through the lanes array:
[CustomEditor(typeof(Section))]
public class SectionEditor : Editor {
Section targetScript;
private SerializedProperty lanes;
void OnEnable()
{
targetScript = (Section)target;
lanes = serializedObject.FindProperty("lanes");
}
public override void OnInspectorGUI()
{
EditorGUILayout.PropertyField(lanes, true);
if (GUI.changed)
serializedObject.ApplyModifiedProperties();
}
}
My first thought on this matter was creating a custom inspector for the Lane class, but I noticed it doesn't work since the class does not inherit MonoBehaviour.
Does anyone have an idea on how to achieve this?
Thank you!
Your answer
Follow this Question
Related Questions
Editable non-monobehaviour objects 1 Answer
Cannot get a class to show up correctly in a custom inspector 0 Answers
Editor extension, How do I Serialized nested class? 0 Answers
Button to change list highlighted in editor 0 Answers
Can not you change the default GUI style of EditorGUI in OnInspectorGUI? 1 Answer