- Home /
How To Draw a PropertyDrawer within a PropertyDrawer
To prefix this I'm going to annoy everyone by saying I'm trying to make a custom inspector for a object with recursive variables. i.e:
class Obj { List<Node> roots; } class Node { Node child; }
To get around this I changed my code to something like this:
class Obj { Dictionary<int, Node> nodes; List<int> roots; } class Node { int id; List<int> children; }
This works great except I've run into a problem. My inspector draws my root node(s) correctly and always gets/sets from/to the Dictionary. However even though the code for drawing the child nodes is the exact same, instead of using the custom property drawer, it draws it using the default drawer. Of course this means the custom code required to add and change my object properties doesn't function correctly.
The following is the code used to display children property drawer. "groupList" is my object name (which is actually within a parent manager object) - don't confuse this with currData.groupList as that refers to the ReorganisibleList object this is all being worked within, "meshGroupIds" is the name of the dictionary, and I get the element at my index within the "values" array of the dictionary. I then call EditorGUI.PropertyField() for this serialized property.
SerializedProperty prop = currData.groupList.serializedProperty.GetArrayElementAtIndex(index); int valuesIndex = MeshGeneratorWindow.manager.groupList.GetGroupIndex(prop.intValue); SerializedProperty mGroupProp = property.serializedObject.FindProperty("groupList").FindPropertyRelative("meshGroupIds").FindPropertyRelative("values").GetArrayElementAtIndex(valuesIndex); prop.isExpanded = EditorGUI.Foldout (rect, prop.isExpanded, new GUIContent("Mesh Group"), true); rect.y += 18f; if (prop.isExpanded) { EditorGUI.PropertyField(rect, mGroupProp, new GUIContent("Group"), true); height += EditorGUI.GetPropertyHeight(mGroupProp, true); }
So my question is, is it possible to use this kind of psuedo-recursion within Unity? There are no errors or warnings provided by Unity and I confirmed that the property path for both properties (root objects and child objects) point to the same place.
Let me know if more code/context is needed.
Note: here is the code for the base group display - it's almost identical.
SerializedProperty prop = baseGroupList.serializedProperty.GetArrayElementAtIndex(index); int valuesIndex = manager.groupList.GetGroupIndex(prop.intValue); SerializedProperty prop = baseGroupList.serializedProperty.GetArrayElementAtIndex(index); int valuesIndex = manager.groupList.GetGroupIndex(prop.intValue); SerializedProperty mGroupProp = serObj.FindProperty("groupList").FindPropertyRelative("meshGroupIds").FindPropertyRelative("values").GetArrayElementAtIndex(valuesIndex); prop.isExpanded = EditorGUI.Foldout (rect, prop.isExpanded, new GUIContent("Mesh Group"), true); rect.y += 18f; if (prop.isExpanded) { EditorGUI.PropertyField(rect, mGroupProp, new GUIContent("Group"), true); height += EditorGUI.GetPropertyHeight(mGroupProp, true); } else { height += 18f; }