Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Glurth · Mar 04, 2016 at 07:16 PM · arraypropertydrawerattribute

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Mar 04, 2016 at 07:51 PM 0
Share

$$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?

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hrondleman · Aug 07, 2016 at 09:22 PM 0
Share

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.

avatar image Glurth hrondleman · Aug 08, 2016 at 04:30 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drawing Order of DecoratorDrawer doesn't work? 1 Answer

problems writing an enum range attribute 1 Answer

Writing PropertyDrawers for functions. 1 Answer

Unity Inspector batching Custom Property Attributes in Arrays 1 Answer

PropertyDrawer inside an array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges