4.3 PropertyDrawers for Arrays

Hey there! Had a custom editor for arrays based on applying a PropertyAttribute to the array.

Under 4.2 we had a nice custom interface which allowed for drag and drop re-ordering and the creation of new elements with a button click. In 4.3, the editor no longer works.

If I put debug output into the PropertyAttribute constructor (not shown in sample), I do not see the debug output when it is applied to an array field. The output does appear when applying the property to a non-array or when manually inspecting attributes on the System.Reflection.FieldInfo.

As field attributes are only constructed when a lookup is done, it seems like Unity, under the hood, is no longer checking for attributes on arrays before selecting a property drawer.

I’ve attached a testbed project that demonstrates the problem. But I’m including my code inline here for quicker skim reading:

This is the Monobehaviour that owns the fields that are not rendered properly.

using UnityEngine;
using System.Collections;

public class HasSomeAttributeProperties : MonoBehaviour {

	public int Integer;
	public string String;
	public MyClazz Clazz;

	[MyAttribute("Totally Custom", "#")]
	public string AttributeString;
	
	[MyAttribute("Also Custom", "->")]
	public string[] AttributeStringArray;
	
	[MyAttribute("Does this work?", "*")]
	public MyClazz AttributeClazz;

	[MyAttribute("This will also not work.", ">")]
	public MyClazz[] AttributeClazzArray;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

This is the attribute, very simple.

using UnityEngine;
using System.Collections;

public class MyAttribute : PropertyAttribute {

	public string Label;
	public string BulletChar;

	public MyAttribute(string label, string bulletChar)
	{
		Label = label;
		BulletChar = bulletChar;
	}
}

And here is the drawer:

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomPropertyDrawer(typeof(MyAttribute))]
public class MyAttributeDrawer : PropertyDrawer {

	public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
	{
		var mine = attribute as MyAttribute;

		GUI.Label(position, mine.BulletChar);
		position = new Rect (position.x + 25, position.y, position.width, position.height);
		GUI.Label(position, mine.Label);
	}
}

As you can see below. The non-array fields are drawn with the property drawer, but the array fields are not:

1419092--74559--$screen.jpg

1419092–74558–$AttributeProblems.unitypackage (7.47 KB)

1419092--74559--$screen.jpg

I’m seeing the same issue with one of my property drawers as well. It seems like the new functionality applies the PropertyAttribute to the array elements and not to the array itself. Is it possible for someone from unity to give a position on this?

Its sad for me, but it seems like this is a feature, not a bug.

https://unity3d.com/unity/whats-new/unity-4.3

And as is the case, there is a new question - how to apply PropertyDrawer attributes to the whole array now?

UPD: the only workaround idea is to make the wrapper class for arrays needed to be custom-drawed, and apply attributes to those classes, not to arrays itself, but this causes excess code refactoring to make old editor works… so the question is still relevant.

Came across this thread while writing my own Property Drawer : Post Here

In there I check if the string property is an array and if so I use ‘prop.propertyPath’ and ‘prop.serializedObject’ to get the array and adjust it accordingly.