- Home /
ReorderableList and object fields
So I have a list, and I am trying to render it in another editor window. I can't use SerializedProperty since the editor I am creating is using Generics and Reflections.
So if I do this:
ReorderableList r = new ReorderableList((IList)value, type, true, false, true, true);
r.onAddCallback = (ReorderableList list) =>
{
Type listType = list.GetType().GetGenericArguments().Single();
object objectToAdd = null;
if (listType.IsSubclassOf(typeof(ScriptableObject)))
objectToAdd = (object)ScriptableObject.CreateInstance(type);
else
objectToAdd = Activator.CreateInstance(listType);
r.list.Add(objectToAdd);
};
r.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
var element = r.list[index];
Type listType = r.list.GetType().GetGenericArguments().Single();
Rect newRect = new Rect(rect.x, rect.y, rect.width + 50, rect.height);
element = (UnityEngine.Object)EditorGUILayout.ObjectField((UnityEngine.Object)element, listType, false);
// element = (UnityEngine.Object)EditorGUI.ObjectField(newRect, (UnityEngine.Object)element, listType, false);
};
r.DoLayoutList();
I get this view, where the object field draws AFTER the reorderable list
If I change drawElementCallback to this, it draw to small:
ReorderableList r = new ReorderableList((IList)value, type, true, false, true, true);
r.onAddCallback = (ReorderableList list) =>
{
Type listType = list.GetType().GetGenericArguments().Single();
object objectToAdd = null;
if (listType.IsSubclassOf(typeof(ScriptableObject)))
objectToAdd = (object)ScriptableObject.CreateInstance(type);
else
objectToAdd = Activator.CreateInstance(listType);
r.list.Add(objectToAdd);
};
r.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
var element = r.list[index];
Type listType = r.list.GetType().GetGenericArguments().Single();
Rect newRect = new Rect(rect.x, rect.y, rect.width + 50, rect.height);
// element = (UnityEngine.Object)EditorGUILayout.ObjectField((UnityEngine.Object)element, listType, false);
element = (UnityEngine.Object)EditorGUI.ObjectField(newRect, (UnityEngine.Object)element, listType, false);
};
So how do I get the best of both world? Also, when I press the Add button, nothing happens. I tried adding a breakpoint, but VS doesn't want to :/
Answer by fzd · Aug 04, 2015 at 10:39 PM
I had the same problem and fixed it like this...it draws the ObjectField inside each ReorderableList element for me:
targetObject = EditorGUI.ObjectField(new Rect(rect.x + 18, rect.y, rect.width - 18, rect.height), targetObject, typeof(GameObject)) as GameObject;
Answer by Cobo3 · Apr 03, 2016 at 04:06 AM
ReorderableList has this limitation in which you cannot use Layout mode. That's why the callback has the Rect parameter.