Custum editor: How do I setup a 3x3 grid of ObjectFields?

I’m new to editor scripting and trying to setup a 3x3 grid of my own Path-objects on the inspector of my Level-object, like this:

[29975-skærmbillede+2014-07-29+kl.+16.11.10.png|29975]

The problem is, that I can’t seem to get the inspector data stored in the double-array. I’ve tried this workaround, but the data gets reset, when I hit the play button:

Level.cs:

using UnityEngine;
using System.Collections;

public class Level : MonoBehaviour {

	private Path[,] pathMatrix = new Path[3,3];
	
	public void SetPath(Path path, int x, int y)
	{
		pathMatrix[x,y] = path;
	}
	
	public Path GetPath(int x, int y)
	{
		return pathMatrix[x,y];
	}

}

LevelEditor.cs:

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(Level))]
public class LevelEditor : Editor
{
	private Level level;
	
	private void OnEnable()
	{
		level = (Level)target;
	}
	
	public override void OnInspectorGUI()
	{	
		EditorGUIUtility.labelWidth = 20f;
		
		GUILayout.BeginHorizontal();
		
		level.SetPath((Path) EditorGUILayout.ObjectField("TL", level.GetPath(0, 0), typeof(Path), true), 0, 0);
		level.SetPath((Path) EditorGUILayout.ObjectField("T ", level.GetPath(1, 0), typeof(Path), true), 1, 0);
		level.SetPath((Path) EditorGUILayout.ObjectField("TR", level.GetPath(2, 0), typeof(Path), true), 2, 0);
		
		GUILayout.EndHorizontal();
		
		GUILayout.BeginHorizontal();
		
		level.SetPath((Path) EditorGUILayout.ObjectField("L", level.GetPath(0, 1), typeof(Path), true), 0, 1);
		level.SetPath((Path) EditorGUILayout.ObjectField(" ", level.GetPath(1, 1), typeof(Path), true), 1, 1);
		level.SetPath((Path) EditorGUILayout.ObjectField("R", level.GetPath(2, 1), typeof(Path), true), 2, 1);
		
		GUILayout.EndHorizontal();
		
		GUILayout.BeginHorizontal();
		
		level.SetPath((Path) EditorGUILayout.ObjectField("BL", level.GetPath(0, 2), typeof(Path), true), 0, 2);
		level.SetPath((Path) EditorGUILayout.ObjectField("B ", level.GetPath(1, 2), typeof(Path), true), 1, 2);
		level.SetPath((Path) EditorGUILayout.ObjectField("BR", level.GetPath(2, 2), typeof(Path), true), 2, 2);
		
		GUILayout.EndHorizontal();
	}
}

Why does the data get wiped when I enter Play-mode?

This is very important to me! Any help or hints in the right direction will be highly appreciated :slight_smile:

2D arrays aren’t supported by the Unity serializer.

You’ll need to use a 1D array. You can do it by changing your Level class like this:

using UnityEngine;
using System.Collections;
 
public class Level : MonoBehaviour {

    public const int Width = 3;
    private Path[] pathMatrix = new Path[9];
 
    public void SetPath(Path path, int x, int y)
    {
        pathMatrix[y * Width + x] = path;
    }
 
    public Path GetPath(int x, int y)
    {
        return pathMatrix[y * Width + x];
    }
 
}

Hi, Unity can’t serialize multi dimension arrays, that’s why the array is not saved/serialized.
You can use a single dimension array (Path[33]) and reference data in it using mypath = Path[ y3 + x ]. You could also define a new class containing the single dimension array and redefine index operator so you could access this array using [,] notation.