- Home /
Custom inspector, a multidimensional array of enums
Hello,
I've been searching everywhere for a solution, but cannot find it. I want to set parameters of some array as prefabs, and decided to use a custom inspector to edit these prefabs (rather than hard coding each individual class object.
Here's what I have:
using UnityEngine;
using System.Collections;
public class Something : MonoBehaviour {
public enum MovementTypes { None, Move, Jump};
public MovementTypes[,] Movement = new MovementTypes[5,5];
public string Name;
}
And my Editor/ has
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(Something))]
public class SomethingEditor : Editor {
public override void OnInspectorGUI()
{
Something myS = (Something)target;
myS.Name = EditorGUILayout.TextField("Type Name", myS.Name);
for (int y = 0; y < 5; y++)
{
EditorGUILayout.BeginHorizontal ();
for (int x = 0; x < 5; x++)
myS.Movement[x, y] = (Something.MovementTypes)EditorGUILayout.EnumPopup(myS.Movement[x, y]);
EditorGUILayout.EndHorizontal ();
}
}
}
This code graces me with the following errors:
NullReferenceException: Object reference not set to an instance of an object SomethingEditor.OnInspectorGUI () (at Assets/Editor/SomethingEditor.cs:17) UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean forceDirty, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect, Boolean eyeDropperDirty) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Inspector/InspectorWindow.cs:1124) UnityEditor.DockArea:OnGUI()
Unexpected top level layout group! Missing GUILayout.EndScrollView/EndVertical/EndHorizontal? UnityEditor.DockArea:OnGUI()
I've tried a bunch of things, and nothing works. I cannot edit the 5x5 array. The only work around I can fathom right now is to make 25 separate variables.......
You're creating a field before calling any of Begin*
methods of EditorGUILayout
.
Are you referring to
myS.Name = EditorGUILayout.TextField("Type Name", myS.Name);
Because that line works fine. The xy loop does not.
Answer by smallbit · Jul 18, 2014 at 12:17 PM
I cannot help with custom editor, but you can setup array of arrays (two dimensional array) of enums in the inspector in the following way.
[System.Serializable]
public class class1{
public enum myEnum {one,two,three};
public myEnum[] row;
}
public class1[] array;
In the inspector you get this :
][1]
Thanks for the suggestion, but I know this is possible. However, I absolutely need the grid layout or it will be very complicated for me.