Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 dbrizov · Jan 12, 2020 at 10:31 AM · propertydrawerarraylist

CustomPropertyDrawer issue with arrays

I am trying to create a [ReorderableList] attribute so that arrays and lists marked with it can become reorderable. I have a CustomPropertyDrawer for the attribute. The problem is that the OnGUI() method is called for each element of the array/list, and not only once for the array/list itself. Is it possible to somehow make the OnGUI() method to be called for the array/list itself and not for each element individually?

 public class MyMonoBehaviour : MonoBehaviour
 {
     [ReorderableList]
     public List<int> list;
 }
 
 [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
 public class ReorderableListAttribute : PropertyAttribute
 {
 }
 
 [CustomPropertyDrawer(typeof(ReorderableListAttribute))]
 public class ReorderableListPropertyDrawer : PropertyDrawer
 {
     private ReorderableList _reorderableList = null;
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return property.isArray
             ? GetList(property).GetHeight()
             : GetPropertyHeight(property) + GetHelpBoxHeight();
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(position, label, property);
         // property.isArray is never true, because the property is an individual element of the list
         if (property.isArray)
         {
             GetList(property).DoList(position);
         }
         else
         {
             string message = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists";
             DrawDefaultPropertyAndHelpBox(position, property, message, MessageType.Warning);
         }
 
         EditorGUI.EndProperty();
     }
 
     private ReorderableList GetList(SerializedProperty property)
     {
         if (_reorderableList == null)
         {
             ReorderableList reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true)
             {
                 drawHeaderCallback = (Rect rect) =>
                 {
                     EditorGUI.LabelField(rect, string.Format("{0}: {1}", property.displayName, property.arraySize), EditorStyles.label);
                 },
 
                 drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
                 {
                     var element = property.GetArrayElementAtIndex(index);
                     rect.y += 1.0f;
                     rect.x += 10.0f;
                     rect.width -= 10.0f;
 
                     EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, 0.0f), element, true);
                 },
 
                 elementHeightCallback = (int index) =>
                 {
                     return EditorGUI.GetPropertyHeight(property.GetArrayElementAtIndex(index)) + 4.0f;
                 }
             };
         }
 
         return _reorderableList;
     }
 }



  1. A lot of people are making a wrapper class that wraps an array inside and they make a reorderable list for the relative serialized property inside the wrapper. That's not how I wanna do it.

  2. I also don't want to create a custom editor

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

0 Replies

· Add your reply
  • Sort: 

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

117 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 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 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

Change Name Of Prefab That Initiates 1 Answer

Identifier Expected Error ArrayList -1 Answers

Question about updating particles in an arraylist 0 Answers

What is the best method to have a list of references to other scripts? 0 Answers

I tried to convert my Dev Console to C# from JS 0 Answers


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