Hello,
I’m trying to do a custom PropertyDrawer for a custom class with a List. To format the list i use the ReordorableList classe provided by Unity.
The problème is when i try to initialise the list, i got the error : Retrieving array size but no array was provided.
Here my PropertyDrawer :
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
[CustomPropertyDrawer(typeof(SpellsEffectsList))]
public class Drawer_SpellsEffectsList : PropertyDrawer {
//_____ PROTECTED PROPERTIES ________________________________________________
protected ReorderableList _reorderableList;
protected SerializedProperty _serializedList;
protected string _headerText = "";
//_____ CONSTRUCTOR _________________________________________________________
~Drawer_SpellsEffectsList () {
// Delete Callback to make sure we don't get memory leaks etc.
this._reorderableList.drawHeaderCallback -= this.DrawHeader;
this._reorderableList.drawElementCallback -= this.DrawElement;
this._reorderableList.onAddCallback -= this.AddItem;
this._reorderableList.onRemoveCallback -= this.RemoveItem;
}
//_____ TRIGGER _____________________________________________________________
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label){
this._reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true);
// => this is where the error occured : Input elements should be an Array SerializedProperty
// Add listeners to draw events
this._reorderableList.drawHeaderCallback += this.DrawHeader;
this._reorderableList.drawElementCallback += this.DrawElement;
this._reorderableList.onAddCallback += this.AddItem;
this._reorderableList.onRemoveCallback += this.RemoveItem;
// Add header
this._headerText = label.text;
// Override logic works on the entire property
EditorGUI.BeginProperty(position, label, property);
this._reorderableList.DoLayoutList();
EditorGUI.EndProperty();
// Apply changes to the serializedProperty
property.serializedObject.ApplyModifiedProperties();
}
//_____ METHODES ____________________________________________________________
/* -------------------------------------------------------------------------------------------------------------------------
* Draws one element of the list
* ------------------------------------------------------------------------------------------------------------------------- */
protected void DrawElement (Rect rect, int index, bool active, bool focused) {
SerializedProperty item = this._serializedList.GetArrayElementAtIndex(index);
SerializedProperty element_Id = item.FindPropertyRelative("_id");
EditorGUI.BeginChangeCheck();
EditorGUI.PropertyField(rect, element_Id);
if (EditorGUI.EndChangeCheck()){
// Set the item as modified and add the modification in the undo action list
Undo.RecordObject(this._serializedList.serializedObject.targetObject, "Cancel item's modification on " + this._serializedList.GetType().ToString() + " reorderable list");
}
}
/* -------------------------------------------------------------------------------------------------------------------------
* Draws the header of the list
* ------------------------------------------------------------------------------------------------------------------------- */
private void DrawHeader(Rect rect){
GUI.Label(rect, this._headerText);
}
/* -------------------------------------------------------------------------------------------------------------------------
* Add an item in the list
* ------------------------------------------------------------------------------------------------------------------------- */
private void AddItem(ReorderableList list){
this._serializedList.InsertArrayElementAtIndex(list.index);
// Set the list as modified and add the modification in the undo action list
Undo.RecordObject(this._serializedList.serializedObject.targetObject, "Cancel Add item on " + this._serializedList.GetType().ToString() + " reorderable list");
}
/* -------------------------------------------------------------------------------------------------------------------------
* Delete an item of the list
* ------------------------------------------------------------------------------------------------------------------------- */
private void RemoveItem(ReorderableList list) {
this._serializedList.DeleteArrayElementAtIndex(list.index);
// Set the list as modified and add the modification in the undo action list
Undo.RecordObject(this._serializedList.serializedObject.targetObject, "Cancel Remove item on " + this._serializedList.GetType().ToString() + " reorderable list");
}
}
Here my SpellsEffectsList :
using System.Collections.Generic;
using System;
[Serializable]
public class SpellsEffectsList : List<SpellEffect> {
// Only some methodes here ...
}
Here how the list is declared :
[SerializeField]
private SpellsEffectsList _spellsEffects = new SpellsEffectsList();
Here how the the propertyDrawer is called from a custom Editor :
public override void OnInspectorGUI () {
// few code ...
EditorGUILayout.PropertyField(serializedSpell.FindProperty("_spellsEffects"), true);
// some other code ...
}
When i’m in debug mode,I have this in Visual Studio :
I don’t understand why i have this problème.
Do you have an idea ?
Thanks for your help.
