I’m trying to use a reorderable list, but I’m having a hard time implementing it in a way that allows multiple objects to be selected. Has anyone managed to create a reorderable list that works well on all targets?
ReorderableList does not have support for multiple selection. I would suggest you don’t use ReorderableList and instead use TreeView
This is very cool, thanks for pointing me to it!
Been a bit, but if anyone wants to better support reorderable lists with multiple selections you can do it pretty easily by not using the SerializedProperty/Object API. Full, perfect support of every multi-edit scenario is probably not achievable, but it’s not hard to support adding/removing elements without losing unique values.
Add an AddDefault() and RemoveAt() function to whatever class the list is displaying (and actually implement them haha. AddDefault should add a default element to the end of your collection and RemoveAt should remove an element at an index).
Then when you initialize your list callbacks, setup the onAdd and onRemove like so (replacing my Palette class with yours, and ignore that the RemoveAt function is a generic TryRemoveAt function in my use-case, just need to do that cuz I’m doing weird stuff)
list.onAddCallback = list =>
{
Undo.RecordObjects(targets, "Add Palette Element");
foreach (var t in targets)
{
if (t is Palette palette)
{
palette.AddDefault();
}
}
serializedObject.SetIsDifferentCacheDirty();
serializedObject.Update();
};
list.onRemoveCallback = list =>
{
if (list.index > -1 && list.index < list.count) // idk if this check is necessary
{
Undo.RecordObjects(targets, "Remove Palette Element");
foreach (var t in targets)
{
if (t is Palette palette)
{
palette.TryRemoveAt<Swatch>(list.index);
}
}
serializedObject.SetIsDifferentCacheDirty();
serializedObject.Update();
}
};
The important part is the last two lines of each callback.
serializedObject.SetIsDifferentCacheDirty();
serializedObject.Update();
The previous lines modify the list in memory, but not their serialized object representation. Calling SetIsDifferentCacheDirty() and Update() forces the serializedObject to update it’s representation of the data after the foreach loop changed it. Haven’t tried it yet, but I imagine adding support for reordering shouldn’t be much harder to add.