【发布时间】:2019-06-04 01:09:07
【问题描述】:
我有一个带有 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 与航点一起重新排序?如果我通过手动改组数组走在正确的轨道上,我该如何正确制作临时副本,以免丢失第一个被覆盖的元素?
【问题讨论】:
-
为什么要使用两个单独的列表?
WaypointObject的实现是怎样的?你不能在同一个班级中使用class WaypointObject { /*whatever it does*/ public List<WaypointObject> connectedWaypoints; }之类的东西吗?
标签: c# unity3d serialization unity-editor