如何在 Unity3d 检查器中显示交错数组



我想做一个交错数组来订购一组航点系统。我的问题是我不知道如何在 Unity 检查器中显示锯齿状数组,以便我可以用我想要的游戏对象(基本上是棋盘游戏的正方形(填充不同的数组。

该游戏是一款棋盘游戏,玩家可以选择不同的路径(例如马里奥派对(。为了做到这一点,我没有制作一个典型的直线航点系统(从A到B(,而是考虑制作几个航点系统,这样玩家就可以在到达十字路口时从一个航点系统"跳"到另一个航点系统。正如我所写,我不知道如何在检查器中显示锯齿状数组,以便我可以正常工作。我试图将 [system.serializable] 放在脚本类上,但它不起作用,数组根本不出现。

public Transform[][] waypointSystems = new Transform[][] 
    {
      new Transform[1],
      new Transform[43],
      new Transform[1],
      new Transform[5],
      new Transform[7]
    };

快速回答:你不能那么简单。Muktidimesnional 和交错数组不序列化。

一种方法可能是将数组的一个维度包装在另一个类中,例如

[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]但是它变得非常复杂。您可能对这篇文章感兴趣

或尝试实现您自己的检查器,将这个线程中的代码片段用作起点

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);
}

最新更新