Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Unity Inspector中创建多维数组?_C#_Unity3d_Serialization_Multidimensional Array_Inspector - Fatal编程技术网

C# 如何在Unity Inspector中创建多维数组?

C# 如何在Unity Inspector中创建多维数组?,c#,unity3d,serialization,multidimensional-array,inspector,C#,Unity3d,Serialization,Multidimensional Array,Inspector,如何在Unity Inspector中创建enum多维数组并使其可序列化,以便我可以从其他脚本调用它 公共枚举颜色{红、蓝、绿、黄、青、白、紫}; 公共整数行=7; 公共int列=4; 公共颜色[,]块颜色; 私人空间{ blockColors=新颜色[行,列]; } 对我来说,手动键入脚本中的所有28种颜色是很耗时的,尤其是当我必须对数百个级别执行此操作时。是否有办法在Inspector中创建一个表以加快工作流 我尝试将blockColorsa[Serializefield]制作成一个[S

如何在Unity Inspector中创建enum多维数组并使其可序列化,以便我可以从其他脚本调用它

公共枚举颜色{红、蓝、绿、黄、青、白、紫};
公共整数行=7;
公共int列=4;
公共颜色[,]块颜色;
私人空间{
blockColors=新颜色[行,列];
}
对我来说,手动键入脚本中的所有28种颜色是很耗时的,尤其是当我必须对数百个级别执行此操作时。是否有办法在Inspector中创建一个表以加快工作流

我尝试将
blockColors
a
[Serializefield]
制作成一个
[Serializefield]
,但它不起作用。
我以前从未尝试过为检查员编写图表。有人能告诉我一个教程,可以帮助我理解如何编写如图所示的图表吗?

你可以为你的类构建一个CustomEditor脚本,然后使用GUI显示你的多维数组,就像你编写普通的OnGUI事件一样

下面是一个简单的伪代码

Loop for every Rows
   Divide inspector width with columns length;
   Loop for every Columns
       Render Custom Field with dividen width;
   End Loop
   Incrase posY for new rows ++;
End Loop
这里有一些链接可以帮助您开始


您需要创建一个自定义编辑器(或者更具体地说,如果您想将其重新用于其他组件,则需要创建CustomPropertDrawer)

创建这样一个表所需的唯一不明显的部分是强制元素按您想要的方式进行布局。一种方法是手动处理Unity提供的position Rect,但有一种非常简单(尽管灵活性稍低)的方法,只需将元素包装在水平/垂直布局组合中。 直观的方法是将元素封装在

GUILayout.BeginHorizontal();
{
  // your elements line 1
}
GUILayout.EndHorizontal(); 
GUILayout.BeginHorizontal();
{
  // your elements line 2 and so on
}
GUILayout.EndHorizontal(); 
但是它有一个缺点-autolayout只会在当前行中获取大量元素,但是如果元素大小发生变化,这将中断垂直对齐。解决方法是首先在布局中包装每列,然后使用水平布局组合垂直条带,如下所示

GUILayout.BeginHorizontal();
{
  GUILayout.BeginVertical();
  {
   // your elements column 1
  }
 GUILayout.EndVertical();
 GUILayout.BeginVertical();
 { 
   // your elements column 2
 }  
 GUILayout.EndVertical();
}
GUILayout.EndHorizontal(); 
括号只是为了清楚起见,它们不起任何作用。
我希望这能有所帮助

多亏了我提供的所有答案,我想出了这个解决方案:

Levels.cs

Editor/LevelEditor.cs

使用UnityEngine;
使用UnityEditor;
[自定义编辑器(类型(级别))]
公共类LevelEditor:Editor{
公共bool showLevels=true;
公共覆盖无效OnInspectorGUI(){
级别=(级别)目标;
EditorGUILayout.Space();
showLevels=EditorGUILayout.Foldout(showLevels,“Levels”(+Levels.allLevels.Length+);
如果(显示级别){
EditorGUI.indentLevel++;
对于(ushort i=0;iusing UnityEngine;

public enum BlockColors {blank, red, blue, green, yellow, cyan, white, purple};

[System.Serializable] public class level {
    #if UNITY_EDITOR
    [HideInInspector] public bool showBoard;
    #endif
    public int rows = 9;
    public int column = 9;
    public BlockColors [,] board = new BlockColors [columns, rows];
}


public class Levels : MonoBehaviour {

    public Level[] allLevels;

}
using UnityEngine;
using UnityEditor;


[CustomEditor(typeof(Levels))]
public class LevelEditor : Editor {

    public bool showLevels = true;

    public override void OnInspectorGUI() {
        Levels levels = (Levels)target;
        EditorGUILayout.Space ();

        showLevels = EditorGUILayout.Foldout (showLevels, "Levels ("+levels.allLevels.Length+")");
        if (showLevels) {
            EditorGUI.indentLevel++;
            for (ushort i = 0; i < levels.allLevels.Length; i++) {

                levels.allLevels[i].showBoard = EditorGUILayout.Foldout(levels.allLevels[i].showBoard, "Board");
                if (levels.allLevels [i].showBoard) {

                    EditorGUI.indentLevel = 0;

                    GUIStyle tableStyle = new GUIStyle ("box");
                    tableStyle.padding = new RectOffset (10, 10, 10, 10);
                    tableStyle.margin.left = 32;

                    GUIStyle headerColumnStyle = new GUIStyle ();
                    headerColumnStyle.fixedWidth = 35;

                    GUIStyle columnStyle = new GUIStyle ();
                    columnStyle.fixedWidth = 65;

                    GUIStyle rowStyle = new GUIStyle ();
                    rowStyle.fixedHeight = 25;

                    GUIStyle rowHeaderStyle = new GUIStyle ();
                    rowHeaderStyle.fixedWidth = columnStyle.fixedWidth - 1;

                    GUIStyle columnHeaderStyle = new GUIStyle ();
                    columnHeaderStyle.fixedWidth = 30;
                    columnHeaderStyle.fixedHeight = 25.5f;

                    GUIStyle columnLabelStyle = new GUIStyle ();
                    columnLabelStyle.fixedWidth = rowHeaderStyle.fixedWidth - 6;
                    columnLabelStyle.alignment = TextAnchor.MiddleCenter;
                    columnLabelStyle.fontStyle = FontStyle.Bold;

                    GUIStyle cornerLabelStyle = new GUIStyle ();
                    cornerLabelStyle.fixedWidth = 42;
                    cornerLabelStyle.alignment = TextAnchor.MiddleRight;
                    cornerLabelStyle.fontStyle = FontStyle.BoldAndItalic;
                    cornerLabelStyle.fontSize = 14;
                    cornerLabelStyle.padding.top = -5;

                    GUIStyle rowLabelStyle = new GUIStyle ();
                    rowLabelStyle.fixedWidth = 25;
                    rowLabelStyle.alignment = TextAnchor.MiddleRight;
                    rowLabelStyle.fontStyle = FontStyle.Bold;

                    GUIStyle enumStyle = new GUIStyle ("popup");
                    rowStyle.fixedWidth = 65;

                    EditorGUILayout.BeginHorizontal (tableStyle);
                    for (int x = -1; x < levels.allLevels [i].columns; x++) {
                        EditorGUILayout.BeginVertical ((x == -1) ? headerColumnStyle : columnStyle);
                        for (int y = -1; y < levels.allLevels [i].rows; y++) {
                            if (x == -1 && y == -1) {
                                EditorGUILayout.BeginVertical (rowHeaderStyle);
                                EditorGUILayout.LabelField ("[X,Y]", cornerLabelStyle);
                                EditorGUILayout.EndHorizontal ();
                            } else if (x == -1) {
                                EditorGUILayout.BeginVertical (columnHeaderStyle);
                                EditorGUILayout.LabelField (y.ToString (), rowLabelStyle);
                                EditorGUILayout.EndHorizontal ();
                            } else if (y == -1) {
                                EditorGUILayout.BeginVertical (rowHeaderStyle);
                                EditorGUILayout.LabelField (x.ToString (), columnLabelStyle);
                                EditorGUILayout.EndHorizontal ();
                            }

                            if (x >= 0 && y >= 0) {
                                EditorGUILayout.BeginHorizontal (rowStyle);
                                levels.allLevels [i].board [x, y] = (BlockColors)EditorGUILayout.EnumPopup (levels.allLevels [i].board [x, y], enumStyle);
                                EditorGUILayout.EndHorizontal ();
                            }
                        }
                        EditorGUILayout.EndVertical ();
                    }
                    EditorGUILayout.EndHorizontal ();

                }

            }
        }
    }
}
if (GUI.changed) {
    Undo.RecordObject(levels, "Edit levels");
    EditorUtility.SetDirty(levels);
}