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: 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.
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).
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.
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.
@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);
}
}
Answer by HP99Studio · Feb 19, 2019 at 09:36 PM
Maybe this is just what you need?
https://assetstore.unity.com/packages/slug/139001
Your answer
Follow this Question
Related Questions
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