I have a custom class called CardEffect which I try to process multiple effects. Since each effect is different, I created a custom property drawer that hides and reveals certain variables depending on which effect (enum) is selected.
I got it to work correctly, however there seems to be a sorting issue with the dropdown box for the enum. When I click on the field for a property it thinks I’m selecting the enum drop down list even though I’m not touching the dropdown list.
I’ve attached a gif to show you.
- Does anyone know how to adjust this sorting so this isn’t a problem?
- Can anyone help me make it so that the dropdown box and variables are just indented 1 space over instead of halfway across the inspector window?
- Can I change Element 0 text to say Effect 0 instead?
Edit:
I realize I forgot to paste my code:
public enum CardEffectType
{
AddHealth,
DestroyObject
}
[System.Serializable]
public class CardEffect
{
public CardEffectType effectType;
public GameObject targetObject;
public int health;
public int damage;
}
}
[CustomPropertyDrawer (typeof(CardEffect))]
public class CardEffectDrawer : PropertyDrawer
{
float baseHeight = EditorGUIUtility.singleLineHeight;
float fieldSpacing = EditorGUIUtility.standardVerticalSpacing;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
// Adjust the height based on properties shown
SerializedProperty effectType = property.FindPropertyRelative("effectType");
int numberOfProperties = 0;
switch (effectType.enumValueIndex)
{
case (int)CardEffectType.AddHealth:
numberOfProperties = 2;
break;
case (int)CardEffectType.DestroyObject:
numberOfProperties = 1;
break;
default:
break;
}
return base.GetPropertyHeight(property, label) + (numberOfProperties * baseHeight) + (numberOfProperties * fieldSpacing);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty(position, label, property);
// Draw label
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
EditorGUI.indentLevel = 0;
SerializedProperty effectType = property.FindPropertyRelative("effectType");
EditorGUI.PropertyField(position, effectType, GUIContent.none);
switch (effectType.enumValueIndex)
{
case (int)CardEffectType.AddHealth:
EditorGUI.PropertyField(new Rect(position.x, position.y + baseHeight + fieldSpacing, position.width, baseHeight), property.FindPropertyRelative("health"));
EditorGUI.PropertyField(new Rect(position.x, position.y + (baseHeight * 2 + fieldSpacing * 2), position.width, baseHeight), property.FindPropertyRelative("targetObject"), new GUIContent("Target"));
break;
case (int)CardEffectType.DestroyObject:
EditorGUI.PropertyField(new Rect(position.x, position.y + baseHeight + fieldSpacing, position.width, baseHeight), property.FindPropertyRelative("damage"));
break;
default:
break;
}
EditorGUI.EndProperty();
}
}