- Home /
Cutom Array Inspector in a Custom Inspector
I want to make a custom inspector for an array of serialized classes, that can be displayed in a custom inspector of a controller class. I have made a limited capability of this, which can't support serializedObject, or at least I don't know how too. So I can't get any UnityEvents, Classes, etc in the Inspector.
Here is my Serialized Class that will be in the Array
[System.Serializable]
public class CustomArrayClassExample
{
public class SubClass
{
GameObject obj1;
string str1;
}
public bool hasObj;
public GameObject obj;
public bool hasUnityEvent;
public UnityEvent unityEvent;
public bool hasSubClass;
public SubClass subClass;
#if UNITY_EDITOR
public void CustomInspector()
{
bool show = false;
//EditorGUI, EditorGIULayout, GUILayout...
EditorGUI.indentLevel++;
hasUnityEvent = EditorGUILayout.Toggle(new GUIContent("Has UnityEvent", "Does this have an associated unity Event"), hasUnityEvent);
if (hasSubClass)
{
// TODO: ADD Unity Event Inspector support
}
hasObj = EditorGUILayout.Toggle(new GUIContent("Has Object", "Does this have an associated object with it"), hasObj);
if (hasObj)
{
obj = (GameObject)EditorGUILayout.ObjectField( new GUIContent("Game Object"), obj, typeof(GameObject), true);
}
hasSubClass = EditorGUILayout.Toggle(new GUIContent("Has SubClass", "Does this have an associated subclass"), hasSubClass);
if (hasSubClass)
{
//ERROR
// subClass = (SubClass) EditorGUILayout.ObjectField(subClass, typeof(SubClass), true);
}
EditorGUI.indentLevel--;
}
#endif
}
And here is my Controller Class:
public class CustomArrayControllerExample : MonoBehaviour {
public int maxArrSize = 25;
public int numSerialClasses = 1;
public List<CustomArrayClassExample> serialClass = new List<CustomArrayClassExample>();
#if UNITY_EDITOR
[CustomEditor(typeof(CustomArrayControllerExample))]
public class MyScriptEditor : Editor
{
bool[] showPositions;
override public void OnInspectorGUI()
{
var myScript = target as CustomArrayControllerExample;
EditorGUILayout.HelpBox(string.Format("The current maximum supported size for an array is {0}. This can be changed in the code by changing\n\tpublic int maxArrSize = 25;", myScript.maxArrSize), MessageType.Warning);
myScript.numSerialClasses = EditorGUILayout.IntField(new GUIContent("Number in Array", "The number of elements present in the array"), myScript.numSerialClasses);
Repaint();
EditorGUI.indentLevel++;
for (int i = 0; i < myScript.numSerialClasses; i++)
{
showPositions[i] = EditorGUILayout.Foldout(showPositions[i], "Serial Class " + i);
if (showPositions[i])
if (Selection.activeTransform)
{
serializedObject.Update();
myScript.serialClass.Add(new CustomArrayClassExample());
myScript.serialClass[i].CustomInspector();
serializedObject.ApplyModifiedProperties();
}
if (!Selection.activeTransform)
{
}
}
EditorGUI.indentLevel--;
serializedObject.Update();
// EditorGUILayout.PropertyField(serializedObject.FindProperty("serialClass"), true);
serializedObject.ApplyModifiedProperties();
}
private void OnEnable()
{
var myScript = target as CustomArrayControllerExample;
showPositions = new bool[myScript.maxArrSize];
}
}
#endif
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
So here is what the Controller looks like in the inspector, as you can see I can toggle different EditorGUILayout fields, but I need to get UnityEvents and Classes:
Answer by Bunny83 · Sep 29, 2017 at 07:12 PM
Your code is a mess. You mix runtime code and editor code inside runtime classes. Also you mix "serializedObject" and "target".Stick with one way.
What's actually the reasson why you want to create a custom inspector? A custom inspector can't change what is actually serialized. It can only change how the serialized data is displayed.
Note that your nested "SubClass" is not marked "Serializable" so the "subClass" field is not serialized.
The editor code in the serialized class enables its custom inspector to be drawn in the Controllers custom editor inspector. So if I toggle a bool in the serial class it displays the GameObject field (in the Controller inspector).
As for the serialized object and target, I am still learning proper techniques, so if you could elaborate on that I would greatly appreciate it.
And I'll fix that note.
O I get what you mean by editor code in a run time class. So the proper practice is put it in "Editor/ControllerEditor"
As for how to get the nested custom editor I am still at a loss for