Will
Will

Reputation: 83

Shuffling nested properties on Unity ReorderableList reorder

I have a CustomEditor with a ReorderableList that displays a nested ReorderableList for each element. When I drag elements in the outer, parent ReorderableList to change their order, the inner lists don't change their order correspondingly. Here is a gif of what happens:

As you can see, the first item always has one Connected Waypoints, and the second item always has two.

This is the Pathing Agent script:

public class PathingAgent : MonoBehaviour
{
  [System.Serializable]
  public class ConnectedWaypointsListContainer
  {
    public List<WaypointObject> connections = new List<WaypointObject>();
  }

  public List<WaypointObject> waypoints = new List<WaypointObject>();
  public List<ConnectedWaypointsListContainer> connectedWaypoints = new List<ConnectedWaypointsListContainer>();
}

These are the relevant parts of the CustomEditor:

  waypointsList = new ReorderableList(serializedObject, serializedObject.FindProperty("waypoints");
  SerializedProperty connectedWaypointsProperty = serializedObject.FindProperty("connectedWaypoints");
  ...
  waypointsList.onReorderCallbackWithDetails = (ReorderableList list, int oldIndex, int newIndex) =>
  {
    connectedWaypointsProperty.arraySize++;
    connectedWaypointsProperty.GetArrayElementAtIndex(connectedWaypointsProperty.arraySize - 1).objectReferenceValue = connectedWaypointsProperty.GetArrayElementAtIndex(oldIndex).objectReferenceValue;
    if(newIndex < oldIndex)
    {
      for(int i = oldIndex; i > newIndex + 1; --i)
      {
        connectedWaypointsProperty.MoveArrayElement(i - 1, i);
      }
      connectedWaypointsProperty.MoveArrayElement(connectedWaypointsProperty.arraySize - 1, newIndex);
    }
    else
    {
      for(int i = oldIndex; i < newIndex - 1; ++i)
      {
        connectedWaypointsProperty.MoveArrayElement(i + 1, i);
      }
      connectedWaypointsProperty.MoveArrayElement(connectedWayointsProperty.arraySize - 1, newIndex);
    }
    if(connectedWaypointsProperty.GetArrayElementAtIndex(connectedWaypointsProperty.arraySize - 1) != null)
    {
      connectedWaypointsProperty.DeleteArrayElementAtIndex(connectedWaypointsProperty.arraySize - 1);
    }
    connectedWaypointsProperty.DeleteArrayElementAtIndex(connectedWaypointsProperty.arraySize - 1);

My attempt was to manually shuffle along the ConnectedWaypointsListContainer(s), which required caching the first one to be overwritten and overwriting the last with that saved data. However, I get an error when I try to duplicate the to-be-cached list as the last element in the serialized array by assigning the objectReferenceValue: "type is not a supported pptr value".

How can I cause the connectedWaypoints to reorder along with the waypoints? If I'm on the right track by shuffling the arrays manually, how do I properly make a temp copy so the first element overwritten isn't lost?

Upvotes: 0

Views: 1284

Answers (1)

Applejag
Applejag

Reputation: 1268

Make sure you are making a call to

serializedObject.ApplyModifiedProperties();

so that the changes applies back to the original object.

The symptom hints that this is the case.


Further reading:
https://docs.unity3d.com/Manual/editor-CustomEditors.html
https://docs.unity3d.com/ScriptReference/SerializedObject.html

Upvotes: 1

Related Questions