Displaying a single item from a SerializedProperty list in the inspector

Hi all

I’m having a very hard time displaying a single list item in my inspector.

I am currently getting and displaying the SerializedProperty like this:

SerializedObject serializedKvp = new SerializedObject(kvp);
SerializedProperty modifierProperty = serializedKvp.FindProperty("m_Value.Array.data[0]");
EditorGUILayout.PropertyField(modifierProperty , true);

This results in an inspector that looks like this:

1621679--99236--$9aa7dedfc0d9e609f50a07df2171924b.png

When I double click on the field I get an inspector that looks like this:

1621679--99237--$b24f232e5f29291570b626e984e73a7e.png

I get the same result with SerializedProperty.GetArrayElementAtIndex.

My questions:

  1. How can I get rid of the “Element 0” label? This isn’t useful in my inspector.

  2. Why am I only getting what looks like an ObjectField that contains my SerializedObject?

  3. Why are the child properties not being drawn, especially as they draw fine when I double click on the field?

Thanks in advance for any advice.

Ves

  1. Use GUIContent.None as a custom label for PropertyField.
    1. Seems like the property is a component reference, not a nested serialized object. Can’t tell without the code.

Hey Jasper

Looks like GUIContent.none is a thing, but it doesn’t return a GUILayoutOption. Can you elaborate on your suggestion?

The “kvp” object looks something like this:

[Serializable]
public sealed class TypeModifierListKvp : ScriptableObject
{
	[SerializeField, HideInInspector]
	private List<Modifier> m_Value;
}

Modifier looks like this:

[Serializable]
public abstract class Modifier : ScriptableObject
{
}

And finally UIWidgetColorModifier looks like this:

[Serializable]
public class UIWidgetColorModifier : Modifier
{
	[SerializeField] private Color m_Color = Color.white;
}

These are all ScriptableObjects, not Components. Does this help?

You need to use the variant of the EditorGUILatout.PropertyField method that has a GUIContent as its second argument.

EditorGUILayout.PropertyField(modifierProperty, GUIContent.none, true);

You are using objects that derive from ScriptableObject. That means they’re independent asset objects and won’t be shown nested. If you want to use nested serialized objects, don’t let Modifier inherit from ScripableObject. The [Serializable] tag is all you need.

GUIContent.none worked great! Thanks a ton!

I’ve removed the ScriptableObject inheritance and it’s created a whole host of other issues, so I’ll come back to this when I’ve resolved those. Thanks!

So the issue seems to be that since making my Modifier class not inherit from ScriptableObject, the following is returning null:

SerializedObject serializedKvp = new SerializedObject(kvp);
SerializedProperty valuesListProperty = serializedKvp.FindProperty("m_Value"); // This is null

My kvp object is unchanged:

[Serializable]
public sealed class TypeModifierListKvp : ScriptableObject
{
    [SerializeField, HideInInspector]
    private List<Modifier> m_Value;
}

This suggests to me that FindProperty only works with ScriptableObject fields (or collections of ScriptableObjects), but surely this can’t be right?

FindProperty works fine, but I don’t know why the m_Value list is now null.

One thing that occurs to me is that Unity serialization doesn’t work with inheritance. It’ll only store the explicitly defined base type. You would need to use SerializedObject for that, but then you’d have to make a sub-asset hierarchy and that’s a whole other can of worms.

Might be a good idea to step back and look at what you’re trying to achieve here. Is this the right approach for it?