Custom PropertyDrawer for attributes on array members

I like the answer provided by It3ration to this question, about how to do a [ReadOnly] attribute for members of a MonoBehavior.

This works great, EXCEPT for array members. For some reason, the Custom property Drawer is ONLY called on the ELEMENTS of the array, which are properly disabled. However the number of elements in the array, remains alterable by the user. (There is actually a screen shot, showing exactly this, in the comments for answer linked above. Additional tests have confirmed the custom property drawer is only being called on the elements of the array.)

So two questions, one that leads to the other:

  1. Why is the custom property drawer ONLY called for the elements of the array, rather than (or in addition to) the array itself?
    1b) How can I call/override the property drawer for the array itself?

  2. (ReadOnly attribute specific:) How can I fix this such that arrays with the [ReadOnly] attribute can be expanded for viewing, but nothing can be changed, including the array length? I’d also like to make the name of the array member turn grey. (I SHOULD be able to figure this out myself, if I can get a good answer to question 1)


“Editor: PropertyDrawer attributes on members that are arrays are now applied to each element in the array rather than the array as a whole. This was always the intention since there is no other way to apply attributes to array elements, but it didn’t work correctly before. Apologies for the inconvenience to anyone who relied on the unintended behavior for custom drawing of arrays.”

So, apparently, the answer is: 1) This cannot be done, by design.

Alas, this still does not explain how say… [HideInInspector] is able to affect the entire array as field (“as a whole”), rather than just it’s members.

IMHO the design choice, instead of “This was always the intention since there is no other way to apply attributes to array elements”, SHOULD have been to apply it to each element, AND the array field itself: not sure why that is impossible, sounded like they were doing the other half of that before the “fix”. They even had to apologize for removing the functionality. Guess I’m missing something.

Unfortunately, the answer here is that this is just the way unity serialises.

I agree with you that this isn’t necessarily the behaviour your expect but it’s rarely a problem and there are ways round it.

First, do you really need to make this ReadOnly? Is is possible to just make the variable private or protected? If not, could you use [HideInInspector]?

Another option, and this is what I normally do, is just extend the editor and provide my own ways of viewing / manipulating the properties. Unity - Manual: Extending the Editor with IMGUI