我有一个带有 ReorderableList 的 CustomEditor,它为每个元素显示一个嵌套的 ReorderableList。当我在外部、父 ReorderableList 中拖动元素以更改它们的顺序时,内部列表不会相应地更改它们的顺序。这是发生的事情的 GIF:
如您所见,第一项总是有一个 Connected Waypoints,第二项总是有两个。
这是路径代理脚本:
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>();
}
这些是 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);
我的尝试是沿着 ConnectedWaypointsListContainer(s) 手动洗牌,这需要缓存要覆盖的第一个并用保存的数据覆盖最后一个。但是,当我尝试通过分配 objectReferenceValue 将要缓存的列表复制为序列化数组中的最后一个元素时出现错误:“type is not a supported pptr value”。
如何使 connectedWaypoints 与航点一起重新排序?如果我通过手动改组数组走在正确的轨道上,我该如何正确制作临时副本,以便不会丢失第一个被覆盖的元素?