Hi there,
I’m trying to create a custom editor, and in doing so i’ve found an interesting behaviour.
I have a custom class “BaseEffect” which correctly serializes, and I can edit the individual items when using fields such as:
[SerializeReference] private List<BaseEffect> turnEndEffects;
However, I later wanted to change the ordering and display of a couple other details in the editor. So I hid it from the inspector, and intead used EditorGUILayout.PropertyField. However, now the elements aren’t editable. They still show up in the list, but there is no ‘expansion’ arrow to modify their fields.
Strangely enough however, if I remove the [HideInInspector] tag, they (expectedly) both show up, but now both have the correct editable fields. But obviosuly I only want the one, and to customise when it is displayed in the inspector.
I presume there is some extra detail I am missing, however I couldn’t find it from my searching.
As you can see, “On End Effects” shows no expansion, despite being implemented exactly the same as “Turn End Effects”.
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using System.Linq;
[CustomEditor(typeof(Condition))]
public class ConditionEditor : Editor
{
private string[] listNames = {"Apply","Turn Start","Turn End","End"};
private string[] childClassNames;
private int selectedListIndex = 0;
private Dictionary<String, SerializedProperty> sProp;
private void OnEnable()
{
// Find all classes that inherit from BaseEffect
Type parentType = typeof(BaseEffect);
List<Type> childTypes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => parentType.IsAssignableFrom(type) && type != parentType)
.ToList();
// Extract class names
childClassNames = childTypes.Select(type => type.Name).ToArray();
// Get extra serialized properties.
sProp = new Dictionary<string, SerializedProperty>
{
{ "canStack", serializedObject.FindProperty("canStack") },
{ "maxStack", serializedObject.FindProperty("maxStack") },
{ "permenant", serializedObject.FindProperty("permenant") },
{ "turnsActive", serializedObject.FindProperty("turnsActive") },
{ "onApplyEffects", serializedObject.FindProperty("onApplyEffects") },
{ "turnStartEffects", serializedObject.FindProperty("turnStartEffects") },
{ "turnEndEffects", serializedObject.FindProperty("turnEndEffects") },
{ "onEndEffects", serializedObject.FindProperty("onEndEffects") },
};
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
// Display the drop-down list
if (target is Condition condition)
{
serializedObject.Update();
// Draw bools and condtional sections.
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
bool canStack = sProp["canStack"].boolValue;
sProp["canStack"].boolValue = EditorGUILayout.Toggle("Can Stack", canStack);
if (canStack)
{
sProp["maxStack"].intValue = EditorGUILayout.IntField("Max Stack", sProp["maxStack"].intValue);
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
bool permenant = sProp["permenant"].boolValue;
sProp["permenant"].boolValue = EditorGUILayout.Toggle("Permenant", permenant);
if (!permenant)
{
sProp["turnsActive"].intValue = EditorGUILayout.IntField("Turns Active", sProp["turnsActive"].intValue);
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
// Draw action lists
EditorGUILayout.PropertyField(sProp["onApplyEffects"], true);
EditorGUILayout.PropertyField(sProp["turnStartEffects"], true);
EditorGUILayout.PropertyField(sProp["turnEndEffects"], true);
EditorGUILayout.PropertyField(sProp["onEndEffects"], true);
selectedListIndex = EditorGUILayout.Popup("Select List:", selectedListIndex, listNames);
int selectedClassIndex = EditorGUILayout.Popup("Add Effect", -1, childClassNames);
if (selectedClassIndex >= 0 && selectedClassIndex < childClassNames.Length)
{
// Create an instance of the selected child class
Type selectedType = Type.GetType(childClassNames[selectedClassIndex]);
if (selectedType != null)
{
object newInstance = Activator.CreateInstance(selectedType);
switch (selectedListIndex)
{
case 0:
condition.OnApplyEffects.Add((BaseEffect)newInstance);
break;
case 1:
condition.TurnStartEffects.Add((BaseEffect)newInstance);
break;
case 2:
condition.TurnEndEffects.Add((BaseEffect)newInstance);
break;
case 3:
condition.OnEndEffects.Add((BaseEffect)newInstance);
break;
}
}
}
serializedObject.ApplyModifiedProperties();
}
}
}
