• 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
2
Question by frarees · Dec 30, 2013 at 07:18 PM · editorarraypropertydrawer4.3

CustomPropertyDrawer for Array types in 4.3

I'm trying to implement a drawer for an array type. The closest I can get is by writing a property drawer for the elements it contains, but not for the array (parent).

 [CustomPropertyDrawer (typeof (PutOnArraysAttribute))]
 // I'll get OnGUI calls for the elements of the array but not for the array itself

This happens on 4.3+. Any ideas?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Arkade · Jun 29, 2014 at 02:08 PM

I believe you cannot create a PropertyDrawer for an Array, only an Editor for the containing class and then PropertyDrawers for the members (like you mentioned).

See http://catlikecoding.com/unity/tutorials/editor/custom-list/.

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 AlkisFortuneFish · Jun 29, 2014 at 02:28 PM 1
Share

To be a tad more precise, you used to be able to. This is the relevant entry in the 4.3 release change log:

 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.
 

It's annoying, as it would be really really useful, especially with 4.5's ReorderableList, you have to jump extra hoops to use it nicely now. I$$anonymous$$O, they should add a way to apply attributes to the arrays themselves and have both behaviours available to us.

avatar image
5

Answer by slippdouglas · Jan 14, 2015 at 11:25 PM

Create a small System.Serializable nested struct/class within your MonoBehaviour to contain your list:

 public class SomeMonoBehaviour : MonoBehaviour
 {
     [Serializable] public struct ThingsWrapper {
         public List<string> list;
         
         // optional, for convenience (so you can use ThingsWrapper as if it's a List):
         public static implicit operator List<string>(ThingsWrapper c) { return c.list; }
         public static implicit operator ThingsWrapper(List<string> l) { return new PointsWrapper(){ list = l }; }
     }
     …

Add a field to your MonoBehaviour of this nested ThingsWrapper type adorned with your PutOnArrays attribute:

     …
     [PutOnArrays]
     public ThingsWrapper things;
     …

(Those implicit operators allows your code to keep using the field as if it were an array/list:)

     …
     void Awake() {
         List<string> things = this.things;
         things.Add("hi");
         Debug.Log(things);
     }
     …
 } // end of SomeMonoBehaviour

Then, in your PutOnArraysDrawer class, use FindPropertyRelative() to work with the list field:

 [CustomPropertyDrawer(typeof(PutOnArraysAttribute))]
 public class PutOnArraysDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         property = property.FindPropertyRelative("list");
         
         // …
     }
     
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         property = property.FindPropertyRelative("list");
         
         // …
     }
 }


This approach both allows you specify custom attribute/drawers for arrays and for their elements, and keeps those attribute associations within your MonoBehaviour (rather than requiring new class files for each use-case).

If you wanted, I'm sure you wrap up ThingsWrapper into a reusable base class to simplify the code in your class— but it's really not that much code in the first place. I've set up my Attribute to take a listFieldName constructor arg and store it in a property, then my Drawer passes ((PutOnArraysAttribute)this.attribute).listFieldName into FindPropertyRelative() (instead of assuming "list"). Where you go with this is up to you.

Comment
Add comment · 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
2

Answer by sotirosn · Mar 17, 2016 at 07:00 AM

I think they should let you put array types into the CustomPropertyDrawer attribute:

 [CustomPropertyDrawer(typeof(Hotkey[]))]
 public class HotkeysDrawer : PropertyDrawer { ... }

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

How to Save an multidimensional array trough editor script 1 Answer

Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer

Show Array Default Value in the Editor 1 Answer

Mixing custom editors with custom property drawers, possible ? 1 Answer

Is a PropertyDrawer "persistent" ? 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