Search Unity

Looping through all SerializedProperties and displaying them with EditorGUILayout.PropertyField?

Discussion in 'Editor & General Support' started by Edvard-D, Aug 1, 2017.

  1. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    129
    So, I'm working on my first custom EditorWindow. In it I'm using GUILayout.Window to draw a list of objects. The list type is an abstract class, so from my understanding, the way to display the properties would be:

    Code (CSharp):
    1. var serializedProperty = serializedObject.GetIterator();
    2. while (serializedProperty.NextVisible(true))
    3. {
    4.     EditorGUILayout.PropertyField(serializedProperty, includeChildren: true);
    5. }
    This works fine with basic types (ints, floats, strings, etc), but there are two problems I've run into:

    1. When I include a Vector3 field in the base class being displayed, it appears as the loop first displays the Vector3 (let's call it _testVector3), and it then tries to display the x value (I have a Debug.Log that displays the name of the serializedProperty). When trying to display the x value I get an "InvalidOperationException: Sequence contains no elements" exception. I don't want to handle how Vector3 fields get displayed, and thought "includeChildren: true" would've solved it.
    2. The class I'm trying to display has a field that's a custom type, and that custom type has a single serialized field (_object). In the picture below you can see that the field seems to be displayed twice (in this case the Vector3 field was commented out). The thing is that both of the fields have the name _object, but the type of the first (with the drop down) is "Generic" while the type of the second is ObjectReference. I'm confused as to why the second "ObjectReference" copy is getting displayed at all.



    I have a feeling that both of these problems have the same root cause, but I haven't had any luck finding similar cases.

    Thanks in advance for your time!
     
  2. Edvard-D

    Edvard-D

    Joined:
    Jun 14, 2012
    Posts:
    129
    Bump!