- Home /
 
Custom Editor Dynamic 2D array
I have a 2D grid that needs to be filled with numbers. I searched and tried multiple solutions online but they either deleted or did not work.
In the first image, when I select Grid Size to be small then I get 4 by 4 matrix or 16 intFields in total.

In the second image, when I select Grid Size to be huge then I get 7 by 7 matrix

The scripts are level and levelEditor. The level script is a scriptable object whereas levelEditor is a custom editor.
The problem here is that the intFields are generated by the levelEditor and they aren't linked to level since I am not knowing how to do it. The second problem is whenever I write anything into the fields , it is stuck to 0.
Here is the code for level script:
 using UnityEngine;
 
 
 [CreateAssetMenu(fileName = "New Level", menuName = "Levels/level", order = 1)]
 public class Level: ScriptableObject
 {
 
     public int number;
     public Constants.Difficulty difficulty;
     public Constants.GridSize gridSize;
    // tried different things here like list of list, 2d arrays and a class (row) of cols then an array of rows 
 
 }
 
               Here is the code for the levelEditor:
 using System;
 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(Level))]
 public class LevelEditor : Editor
 {
     public override void OnInspectorGUI()
     {
 
         Level l = (Level) target;
 
         int[,] fieldsArray = new int[0, 0];
 
         EditorGUILayout.BeginHorizontal();
             GUILayout.Label("Number");
             GUILayout.Label("Difficulty");
             GUILayout.Label("Grid Size");
         EditorGUILayout.EndHorizontal();
 
         EditorGUILayout.BeginHorizontal();
             l.number = EditorGUILayout.IntField(l.number);
             l.difficulty = (Constants.Difficulty) EditorGUILayout.EnumPopup(l.difficulty);
             l.gridSize = (Constants.GridSize)EditorGUILayout.EnumPopup(l.gridSize);
         EditorGUILayout.EndHorizontal();
         
 
         int gridSize = 4;
         if (l.gridSize == Constants.GridSize.Medium) gridSize = 5;
         else if (l.gridSize == Constants.GridSize.Large) gridSize = 6;
         else if (l.gridSize == Constants.GridSize.Huge) gridSize = 7;
         //below is of interest, also tried a lot of solutions 
         GUILayout.Label("Equation");
 
         if (gridSize != fieldsArray.GetLength(0) || gridSize != fieldsArray.GetLength(1))
         {
             fieldsArray = new int[gridSize, gridSize];
         }
 
         for (int j = 0; j < gridSize; j++)
         {
             EditorGUILayout.BeginHorizontal();
             for (int i = 0; i < gridSize; i++)
             {
                 fieldsArray[i, j] = EditorGUILayout.IntField(fieldsArray[i, j]);
             }
             EditorGUILayout.EndHorizontal();
         }
 
     }
 }
 
              Answer by Map-Builder · May 29, 2020 at 01:03 AM
      int[,] fieldsArray = new int[0, 0];
 
               it is called each frame in GUI, so created each frame and destroyed at the end of function. just put it outside ? XD
Your answer
 
             Follow this Question
Related Questions
custom button 2 Answers
Custom control in editor. 1 Answer
How to limit an ObjectField to assets with specific file extension 2 Answers