如何在Unity Inspector中创建多维数组及可序列化枚举多维数组
Great question! Unity's Inspector doesn't natively play nice with multi-dimensional arrays, but we can fix this with serialization workarounds and handy editor tools to cut down on that tedious manual setup. Let's tackle your problems one by one:
1. Displaying Multi-Dimensional Arrays in the Inspector
Unity won't render a [,] multi-dimensional array in the Inspector by default, so we'll wrap our rows in a serializable class to make it editable. Here's how to adapt your code:
using UnityEngine; using System; // Follow C# naming conventions for enums (PascalCase) public enum Colors { Red, Blue, Green, Yellow, Cyan, White, Purple }; // This serializable class lets Unity display each row of colors in the Inspector [Serializable] public class ColorRow { public Colors[] Columns; } public class LevelColorManager : MonoBehaviour { public int TotalRows = 7; public int TotalColumns = 4; // Replace your multi-dimensional array with this serialized row array public ColorRow[] BlockColorRows; private void Awake() { // Optional: Convert the row array to a multi-dimensional array if needed InitializeMultiDimensionalArray(); } // We'll use this later for other scripts to access the data private Colors[,] _multiDimensionalColors; private void InitializeMultiDimensionalArray() { _multiDimensionalColors = new Colors[TotalRows, TotalColumns]; for (int row = 0; row < TotalRows; row++) { if (BlockColorRows[row] != null && BlockColorRows[row].Columns.Length >= TotalColumns) { for (int col = 0; col < TotalColumns; col++) { _multiDimensionalColors[row, col] = BlockColorRows[row].Columns[col]; } } } } }
Now in the Inspector, you'll see BlockColorRows as an array you can resize. Each element is a ColorRow that you can expand to edit the Columns array of your Colors enum values.
2. Making the Enum Array Accessible to Other Scripts
To let other scripts access your color data, add a public getter method or property to your LevelColorManager class. This ensures other scripts get a valid, initialized multi-dimensional array:
// Add this inside LevelColorManager public Colors[,] GetBlockColors() { // Lazy initialization: only create the array if it doesn't exist yet if (_multiDimensionalColors == null) { InitializeMultiDimensionalArray(); } return _multiDimensionalColors; }
Now any other script can access the data like this:
// In another script LevelColorManager colorManager = FindObjectOfType<LevelColorManager>(); if (colorManager != null) { Colors[,] levelColors = colorManager.GetBlockColors(); // Use the colors here, e.g.: Colors targetColor = levelColors[2, 3]; }
3. Streamlining Bulk Setup for Hundreds of Levels
Manually setting 28 colors per level is no fun—here are two ways to speed this up:
Option 1: Editor Tools for One-Click Generation
Add a custom editor script to add buttons that generate random colors or clear all values in the Inspector. Create a new folder called Editor (Unity treats scripts here as editor-only) and add this:
using UnityEditor; using UnityEngine; using System; [CustomEditor(typeof(LevelColorManager))] public class LevelColorManagerEditor : Editor { public override void OnInspectorGUI() { base.OnInspectorGUI(); LevelColorManager manager = (LevelColorManager)target; // Button to generate random colors for all cells if (GUILayout.Button("Generate Random Colors")) { manager.BlockColorRows = new ColorRow[manager.TotalRows]; for (int row = 0; row < manager.TotalRows; row++) { manager.BlockColorRows[row] = new ColorRow(); manager.BlockColorRows[row].Columns = new Colors[manager.TotalColumns]; for (int col = 0; col < manager.TotalColumns; col++) { // Pick a random enum value manager.BlockColorRows[row].Columns[col] = (Colors)Random.Range(0, Enum.GetValues(typeof(Colors)).Length); } } // Mark the object as modified so Unity saves the changes EditorUtility.SetDirty(manager); } // Button to clear all colors to the default enum value if (GUILayout.Button("Clear All Colors")) { manager.BlockColorRows = new ColorRow[manager.TotalRows]; for (int row = 0; row < manager.TotalRows; row++) { manager.BlockColorRows[row] = new ColorRow(); manager.BlockColorRows[row].Columns = new Colors[manager.TotalColumns]; } EditorUtility.SetDirty(manager); } } }
Now you'll see two new buttons in the Inspector for your LevelColorManager component—click "Generate Random Colors" to populate all cells instantly!
Option 2: Preset Reuse
If you have common color patterns (e.g., a row of alternating Red/Blue), set up one ColorRow with that pattern, right-click it in the Inspector, and select "Copy". Then you can paste it into other rows or even other level objects to reuse the configuration.
内容的提问来源于stack exchange,提问作者user1105430

