Hey everyone,
so while trying to draw an inspector within another inspector I almost decided to jump out of the window, but instead decided to post a thread. Maybe some Unity Dev or UI Guru can help me out. I’m on 2019.1.14f1.
The TL;DR is: using InspectorElement doesn’t create PropertyFields, but instead they are empty. See this image:

The detailed version: I created a few simple scripts to test how to do it. The premise is simple: Draw the members from TestComponent2 in the Inspector of TestComponent1.
public class TestComponent1 : MonoBehaviour
{
public int TestValue1 = 15;
public TestComponent2 RefToComponent;
}
public class TestComponent2 : MonoBehaviour
{
public int TestValue2 = 55;
}
I have then created a simple CustomEditor for TestComponent2:
[CustomEditor(typeof(TestComponent2))]
public class TestComponent2Editor : Editor
{
public override VisualElement CreateInspectorGUI()
{
VisualElement root = new VisualElement();
root.Add(new PropertyField(serializedObject.FindProperty("TestValue2")));
return root;
}
}
So far so good because eerything works as intended. Now to the apparently tricky stuff: I want to display the above inspector in the inspector of TestComponent1.
[CustomEditor(typeof(TestComponent1))]
public class TestComponent1Editor : Editor
{
public override VisualElement CreateInspectorGUI()
{
VisualElement root = new VisualElement();
var prop = serializedObject.FindProperty("RefToComponent");
root.Add(new PropertyField(serializedObject.FindProperty("TestValue1")));
root.Add(new PropertyField(prop));
if (prop.objectReferenceValue != null)
root.Add(new InspectorElement(prop.objectReferenceValue));
return root;
}
}
The important bit is this line:
root.Add(new InspectorElement(prop.objectReferenceValue));
This innocent line actually doesn’t really do anything. It creates the necessary elements but the PropertyField’s are empty. There is nothing attached to it. How do I work around this issue?