- Home /
How to show an array of custom objects in Inspector?
I can show one object, but not an array of them.
BasicLight in Inspector: (I want to show an array of these)
LightTube contains an array of BasicLights and looks like this: (This does not work, I want to show an array of four fields each)
How do I get an array of BasicLights to show the fields of each element (that we see in the first image)? I have also tried making a CustomPropertyDrawer (not shown here), but I haven't been able to get it to work with the custom inspector. Any suggestions?
Code is below.
BasicLight (a basic light, of which arrays will be made of in the inspector)
[Serializable]
public class BasicLight : MonoBehaviour
{
public bool isLightOn = false;
public string name;
public GameObject light;
public float range = 30.0f;
public float intensity = 1.5f;
}
EditBasicLight (creates the GUI controls in Inspector)
[CustomEditor(typeof(BasicLight))]
public class EditBasicLight : Editor
{
public override void OnInspectorGUI ()
{
serializedObject.Update ();
BasicLight basicLight = target as BasicLight;
basicLight.isLightOn = EditorGUILayout.Toggle ("on", basicLight.isLightOn);
EditorGUI.indentLevel++;
basicLight.name = EditorGUILayout.TextField ("name", basicLight.name);
basicLight.light = EditorGUILayout.ObjectField ("light", basicLight.light);
basicLight.range = EditorGUILayout.FloatField ("range", basicLight.range);
basicLight.intensity = EditorGUILayout.FloatField ("intnsity", basicLight.intensity);
EditorGUI.indentLevel--;
}
}
BaseLight (LightTube inherits from this) (attached to the GameObject) (Other classes will extend this, hence the name ‘Base’Light.)
public class BaseLight : MonoBehaviour
{
public bool on = false;
public BasicLight [] lights;
}
EditBaseLight (BaseLight Editor. Creates the GUI controls in Inspector) (But how do you get the array to show correctly)
[CustomEditor(typeof(BaseLight))]
public class EditBaseLight : Editor
{
public override void OnInspectorGUI ()
{
serializedObject.Update ();
BaseLight baseLight = target as BaseLight;
EditorGUIUtility.LookLikeInspector ();
SerializedObject serializedObj = new SerializedObject (target);
SerializedProperty lights = serializedObject.FindProperty ("lights");
serializedObj.Update ();
EditorGUILayout.HelpBox ("Default Inspector", MessageType.None);
DrawDefaultInspector();
serializedObj.ApplyModifiedProperties ();
}
}
LightTube
public class LightTube : BaseLight
{
}
EditLightTube
[CustomEditor(typeof(LightTube))]
public class EditLightTube : EditBaseLight
{
public override void OnInspectorGUI()
{
LightTube lightTube = target as LightTube;
base.OnInspectorGUI ();
}
}
I also tried putting [SerializeField] before every variable in the BasicLight but there was no change.
Answer by dtumolo · Jul 29, 2017 at 05:51 AM
When I add "[System.Serializable]" in front of my class definition, it works.
Answer by Eye_of_Triad · Jul 14, 2017 at 02:18 PM
I discovered that if I remove the extension from MonoBehaviour, it shows the fields for each array element in Inspector.
However, I want to do special checks for input (eg when they check a box, turn on a light). So when I create the CustomEditor class, it requires a MonoBehaviour object.
So I'm stuck choosing one or the other. Either I can have the array show it correctly, or I can have a custom editor.
Is there any way to have both?