如何在 Unity3d 检查器中显示锯齿状数组?
How to show a jagged array in Unity3d inspector?
我想制作一个锯齿状数组来订购一组航路点系统。我的问题是我不知道如何在 Unity 检查器中显示锯齿状数组,以便我可以用我想要的游戏对象(基本上是棋盘游戏的方块)填充不同的数组。
该游戏是一款棋盘游戏,玩家可以选择不同的路径(例如马里奥派对)。为了做到这一点,我没有制作典型的直线路点系统(从 A 到 B),而是考虑制作多个路点系统,以便玩家在到达十字路口时可以 'jump' 从一个路点系统到另一个路点系统。正如我所写,我不知道如何在检查器中显示锯齿状的数组,以便我可以正常工作。我试图将 [system.serializable] 放在脚本 class 上,但它不起作用,数组根本就没有出现。
public Transform[][] waypointSystems = new Transform[][]
{
new Transform[1],
new Transform[43],
new Transform[1],
new Transform[5],
new Transform[7]
};
快速回答:你不能这么简单。多维度和锯齿状数组未序列化。
一种方法是将数组的一维包装在另一个 class 中,例如
[Serializable]
public class TransformArray
{
public Transform[] Array;
public TransformArray(Transform[] array)
{
Array = array;
}
}
public TransformArray[] waypointSystems = new TransformArray[]
{
new TransformArray(new Transform[1]),
new TransformArray(new Transform[43]),
new TransformArray(new Transform[1]),
new TransformArray(new Transform[5]),
new TransformArray(new Transform[7])
};
或者你可以写一个[CustomEditor]
but that there it gets really complex. You might be interested in
或者尝试使用 this thread 中的代码片段作为起点来实现您自己的检查器
SerializedProperty data = property.FindPropertyRelative("rows");
for (int x = 0; x < data.arraySize; x++)
{
// do stuff for each member in the array
EditorGUI.PropertyField(newPosition, data.GetArrayElementAtIndex(x), GUIContent.none);
}
我想制作一个锯齿状数组来订购一组航路点系统。我的问题是我不知道如何在 Unity 检查器中显示锯齿状数组,以便我可以用我想要的游戏对象(基本上是棋盘游戏的方块)填充不同的数组。
该游戏是一款棋盘游戏,玩家可以选择不同的路径(例如马里奥派对)。为了做到这一点,我没有制作典型的直线路点系统(从 A 到 B),而是考虑制作多个路点系统,以便玩家在到达十字路口时可以 'jump' 从一个路点系统到另一个路点系统。正如我所写,我不知道如何在检查器中显示锯齿状的数组,以便我可以正常工作。我试图将 [system.serializable] 放在脚本 class 上,但它不起作用,数组根本就没有出现。
public Transform[][] waypointSystems = new Transform[][]
{
new Transform[1],
new Transform[43],
new Transform[1],
new Transform[5],
new Transform[7]
};
快速回答:你不能这么简单。多维度和锯齿状数组未序列化。
一种方法是将数组的一维包装在另一个 class 中,例如
[Serializable]
public class TransformArray
{
public Transform[] Array;
public TransformArray(Transform[] array)
{
Array = array;
}
}
public TransformArray[] waypointSystems = new TransformArray[]
{
new TransformArray(new Transform[1]),
new TransformArray(new Transform[43]),
new TransformArray(new Transform[1]),
new TransformArray(new Transform[5]),
new TransformArray(new Transform[7])
};
或者你可以写一个[CustomEditor]
but that there it gets really complex. You might be interested in
或者尝试使用 this thread 中的代码片段作为起点来实现您自己的检查器
SerializedProperty data = property.FindPropertyRelative("rows");
for (int x = 0; x < data.arraySize; x++)
{
// do stuff for each member in the array
EditorGUI.PropertyField(newPosition, data.GetArrayElementAtIndex(x), GUIContent.none);
}