- Home /
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. http://answers.unity3d.com/questions/489942/how-to-make-a-readonly-property-in-inspector.html
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)
Answer by phil_me_up · Mar 04, 2016 at 07:27 PM
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. http://docs.unity3d.com/Manual/ExtendingTheEditor.html
$$anonymous$$y objective is to show the user results of generated data, so simply hiding it, unfortunately, wont work here.
A custom editor for the monobehavior is indeed my fallback option, but that kinda defeats the whole point of having a custom Attribute and PropertyDrawer.
" just the way unity serialises "
I guess I don't understand what this is implying. Obviously the array's size IS being serialized, since it gets displayed. If [HideInInspector] will be able to effect the way the entire array is drawn, not just the elements, why can't my custom attribute and property drawer do the same?
Answer by Glurth · Mar 05, 2016 at 09:53 PM
From https://unity3d.com/unity/whats-new/unity-4.3
"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.
Did you happen to find a workaround for this? I need my size to be adjusted by another property which works, but it resets the array if you accidentally type anything in the size box and I'd like to stop that.
nothing automatic. Created a function to draw array-type SerializedProperties they way I wanted based on current state of GUI.enabled, and called that function ins$$anonymous$$d of PropertyField(), in my custom editors. Ignored the [ReadOnly] flag.