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