Question by
potato_dev · Jun 04, 2020 at 06:43 PM ·
inspectoreditor-scriptinginheritancecustom editorpopup
Popup to choose which child class I want in Custom Editor Inspector
Hello, I have something like this:
and then a ScritableObject:[Serializable] public class Shape{ }
[Serializable] public class Circle : Shape { public float radius; }
[Serializable] public class Rectangle : Shape { public float width; public float height; }
[CreateAssetMenu]
public class MyScriptable : ScriptableObject{
public Shape[] shapes;
}
What I want is a popup in my inspector to select each
Shape in my array. So what I tried:
[CustomEditor(typeof(MyScriptable))]
public class MyEditor : Editor {
SerializedProperty shapes;
void OnEnable() {
shapes = serializedObject.FindProperty("shapes");
}
public override void OnInspectorGUI() {
serializedObject.Update();
EditorGUILayout.PropertyField(shapes);
serializedObject.ApplyModifiedProperties();
}
}
and to show the popup I did this:
[CustomPropertyDrawer(typeof(Shape))]
public class ShapeDrawer : PropertyDrawer {
string[] options = {"Circle", "Rectangle"};
private int index = 0;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
index = EditorGUI.Popup(position, index, options);
}
}
But for some reason, when I select an option on one element, the selection occurs on all other elements. And I couldn't find a way to create the class on the type when I select. Plus of course I want to the inspector display all attributes of the selected class below the popup. Here is what I get:
Does anyone know how to make this?
スクリーンショット-2020-06-04-午後34048.png
(50.0 kB)
Comment
Your answer
Follow this Question
Related Questions
Custom Editor List with child classes 1 Answer
How to edit array/list property with custom PropertyDrawer? 3 Answers
How to add Texture2D to Popup/Dropdown 0 Answers
Custom Editor Handle Changes Not Persisting in Play Mode. 1 Answer
Update gameObject's children properties from CustomEditor class 1 Answer