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 /
  • Help Room /
avatar image
2
Question by thiezar · Mar 12, 2018 at 11:11 PM · propertydrawerarray list

How to properly use ReorderableList inside CustomPropertyDrawer

I want to show a ReorderableList for every item of an array using a CustomPropertyDrawer. Here's my model:


Library.cs

 [Serializable]
 [CreateAssetMenu(fileName = "myLibrary", menuName = "Library", order = 0)]
 public class Library : ScriptableObject
 {
     public string title;
     public List<Book> books = new List<Book>();
 }
 
 [Serializable]
 public class Book
 {
     public string title;
     public List<Chapter> chapters;
 }
 
 [Serializable]
 public class Chapter
 {
     public string title;
 }

As you can see it's a simple library with a list of books, and every book has a list of chapters. I will use a CustomEditor for the library and a CustomPropertyDrawer for each book. I would like to use a ReorderableList for the chapters of every book.


LibraryEditor.cs

 [CanEditMultipleObjects]
 [CustomEditor(typeof(Library))]
 public class LibraryEditor : Editor
 {
     private ReorderableList bookList;
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         EditorGUILayout.PropertyField(serializedObject.FindProperty("title"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("books"), true);
         serializedObject.ApplyModifiedProperties();
     }
 }

BookDrawer.cs

 [CustomPropertyDrawer(typeof(Book))]
 public class BookDrawer : PropertyDrawer
 {
     ReorderableList chapterList;
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         float height = 0;
         EditorGUI.BeginProperty(position, label, property);
         {
             Rect titleRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
             EditorGUI.PropertyField(titleRect, property.FindPropertyRelative("title"));
             height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
 
             Rect chaptersRect = new Rect(position.x, position.y + height, position.width, EditorGUIUtility.singleLineHeight);
             if(chapterList == null)
             {
                 chapterList = BuildChaptersReorderableList(property.FindPropertyRelative("chapters"));
             }
             chapterList.DoList(chaptersRect);
         }
         EditorGUI.EndProperty();
     }
 
     private ReorderableList BuildChaptersReorderableList(SerializedProperty property)
     {
         ReorderableList list = new ReorderableList(property.serializedObject, property, true, true, true, true);
 
         list.drawHeaderCallback = (Rect rect) =>
         {
             EditorGUI.LabelField(rect, "Chapters");
         };
         list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
             EditorGUI.PropertyField(rect, property.GetArrayElementAtIndex(index), true);
         };
         return list;
     }
 }


Here's the final result: alt text As you can see the problem is that when I add/remove/edit a chapter on a book, all the other books update with the same data. I think this is because Unity uses the same PropertyDrawer instance for every Book in the array.

Does someone knows how can I avoid this?

How can I tell Unity to use a new BookDrawer instance for every book element in the array?

Thank you.

unity-drawer.png (8.8 kB)
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
3
Best Answer

Answer by Adam-Mechtley · Mar 13, 2018 at 08:37 AM

Your assumption is correct. Unity uses the same PropertyDrawer instance for elements in a list or array. You can see my answers here and here to related questions, but the idea is that any view data you need to cache should be stored per property path (i.e. in a string-keyed dictionary field).

Comment
Add comment · Show 3 · 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 thiezar · Mar 13, 2018 at 09:00 PM 0
Share

Sir, you saved my life. I did a lot of research on that but never found your answers. I think Unity doesn't like us to put state variables inside the PropertyDrawer but I really find awful to put view data inside the model class and also it is not easy to access them from the PropertyDrawer. In conlcusion I am happy to use your dictionary method when Unity doesn't provide a proper solution.

avatar image Ahim13 thiezar · Feb 27, 2020 at 03:48 PM 0
Share

Hi, could you elaborate how you solved it with your own model you wrote in this thread? Sorry to bother you but I'm not that well versed in custom inspector scripting and the answer Adam wrote did not really help me understand how to solve it.

avatar image KevinCastejon Ahim13 · Jun 27, 2021 at 03:38 PM 0
Share

@Ahim13 here is an example:

 [Serializable]
 public class MyData
 {
     [SerializeField] private int _myInt;
     [SerializeField] private bool _myBool;
     [SerializeField] private float _myFloat;
 }
 
 [Serializable]
 public class MyDataList
 {
     [SerializeField] private List<MyData> _myDataList;
 }
 
 public class Demo : MonoBehaviour
 {
     [SerializeField] private MyDataList _data;
 }
 
 [CustomPropertyDrawer(typeof(GameConditionList))]
 public class MyDataListDrawer : PropertyDrawer
 {
     private Dictionary<string, ReorderableList> _reorderableLists = new Dictionary<string, ReorderableList>();
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         SerializedProperty myDataList = property.FindPropertyRelative("_myDataList");
         if (!_reorderableLists.ContainsKey(property.propertyPath) || _reorderableLists[property.propertyPath].index > _reorderableLists[property.propertyPath].count - 1)
         {
             _reorderableLists[property.propertyPath] = new ReorderableList(myDataList.serializedObject, myDataList, true, true, true, true);
         }
         return _reorderableLists[property.propertyPath].GetHeight();
     }
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         position = EditorGUI.IndentedRect(position);
         _reorderableLists[property.propertyPath].DoList(position);
     }
 }
 


avatar image
0

Answer by HP99Studio · Feb 19, 2019 at 09:36 PM

Maybe this is just what you need?
https://assetstore.unity.com/packages/slug/139001

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.

Follow this Question

Answers Answers and Comments

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

ApplyModifiedProperties() ignored on nested serialized property w custom editor and custom property drawer 2 Answers

I'm trying to insert input field text into an arraylist 0 Answers

CustomPropertyDrawer for a concrete class of a generic abstract 2 Answers

Is there a way to pass instance values to a custom PropertyAttribute? 1 Answer

Inheritance, List and PropertyDrawer,CustomPropertyDrawer and Inheritance 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