PropertyDrawer for Unity's NativeCollections? (NativeArray, NativeList)

Hi everyone, I’m working on a PropertyDrawer to allow a NativeList to be displayed in the Inspector. Here is what I have right now:

/// <summary>
/// PropertyDrawer for a NativeList
/// </summary>
[CustomPropertyDrawer(typeof(NativeList<TestNodeData>))]
public sealed class NativeListPropertyDrawer : PropertyDrawer
{
    #region Public methods

    /// <summary>
    /// Called when the UI is drawn
    /// </summary>
    /// <param name="position">The position of the field</param>
    /// <param name="property">The property to serialize</param>
    /// <param name="label">The text</param>
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginChangeCheck();

        bool fold = true;
        this.DrawPropertyArray(property, ref fold);

        if (EditorGUI.EndChangeCheck())
        {
            //property.boxedValue = new FixedString4096Bytes(fixedStringValue);
        }
    }

    /// <summary>
    /// Ensures the field will stay at the proper position
    /// </summary>
    /// <param name="property">The property</param>
    /// <param name="label">The text</param>
    /// <returns>The proper height of the field</returns>
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return EditorGUI.GetPropertyHeight(property, label, true);
    }

    #endregion

    #region Private methods

    private void DrawPropertyArray(SerializedProperty property, ref bool fold)
    {
        fold = EditorGUILayout.Foldout(fold, property.displayName, true);
        if (fold)
        {
            SerializedProperty arraySizeProp = property.FindPropertyRelative("TestNodeDatas.Length");
            EditorGUILayout.PropertyField(arraySizeProp);

            EditorGUI.indentLevel++;

            for (int i = 0; i < arraySizeProp.intValue; ++i)
            {
                EditorGUILayout.PropertyField(property.GetArrayElementAtIndex(i));
            }

            EditorGUI.indentLevel--;
        }
    }

    #endregion
}

However, OnGUI() is not even called. It seems the PropertyDrawer doesn’t recognizes the type passed in parameter to the CustomPropertyDrawer attribute. Using the official documentation for Serialization, I tried implementing a binary adapter for a NativeList, hoping this would be the cause of my issue, but nothing changed.

If anyone’s been working on a similar project, please let me know how you managed to serialize and display a nativeCollection in the Inspector.

I think the serialization package and its adapters only do anything when you serialize via said package. It doesn’t have an effect on the default serialization of assets.

Nonetheless if a type is not inherently serializable, then a PropertyDrawer isn’t going to make it serializable and naturally won’t be able to draw it as well. A serializable wrapper class/struct is generally required in those cases, though of course makes usage more cumbersome.

Mind you, and while I’m not super familiar with the Collections package, I think the nature of a NativeList makes it really not suitable for serialization?

Pretty sure that’s the case. Serializing a list of pointers is rather pointless. It can also easily get invalidated by a structure change, as far as I know.

Thank you guys for your answers. I’m not familiar with the serialization process, but I guess that outcome was to be expected. I guess I have to stick with regular collections then.

HI, I am trying draw an UnsafeList and I’ve implemented property drawer (simple label) but nothing is showing up! Have you tried anything recently?

If a type is not inherently serializable by Unity, a property drawer doesn’t change that fact.

1 Like