SagiZiv
SagiZiv

Reputation: 1040

Custom Editor Inheritance in Unity

SOLVED, the fix is in the code

I have some class that I want to create custom editor for, but I can't make it work.

I classes inherit from each other, and I want that the editor for the base class would be applied to all the sub classes editors

I have tried to use [CustomEditor( typeof( BaseClassName ), true )], according to Unity documentation it suppose to work.

But yet, only the base class has a custom editor, and the sub classes get the custom editor I generated from them, ignoring the base class inspector.

I have also tried to inherit from the custom editor class that I created for the base class, but it didn't work…

Base Class:

[CustomEditor(typeof(ScriptableObjects.CharacterData), true)]
public class CharacterEditor : Editor
{
    private SerializedProperty characterName, characterTexture, characterNormalMap;
    private bool texturesFoldout = false;

    protected void OnEnable()
    {
        characterName = serializedObject.FindProperty("characterName");
        characterTexture = serializedObject.FindProperty("characterTexture");
        characterNormalMap = serializedObject.FindProperty("characterNormalMap");
    }
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(characterName);
        texturesFoldout = EditorGUILayout.Foldout(texturesFoldout, "Textures");
        if (texturesFoldout)
        {
            EditorGUILayout.PropertyField(characterTexture);
            EditorGUILayout.PropertyField(characterNormalMap);
        }
        serializedObject.ApplyModifiedProperties();
    }
}

Sub Class:

[CustomEditor(typeof(ScriptableObjects.SoldierData))]
public class SoldierEditor : CharacterEditor
{
    private SerializedProperty life, autoAttack, skills;

    protected new void OnEnable()
    {
        base.OnEnable();
        life = serializedObject.FindProperty("life");
        autoAttack = serializedObject.FindProperty("autoAttack");
        skills = serializedObject.FindProperty("skills");
    }
    public override void OnInspectorGUI()
    {
        // base.DrawDefaultInspector(); The mistake
        base.OnInspectorGUI(); // The FIX!
        serializedObject.Update();
        EditorGUILayout.PropertyField(life);
        EditorGUILayout.PropertyField(autoAttack);
        EditorGUILayout.PropertyField(skills);
        serializedObject.ApplyModifiedProperties();
    }
}

Images:

The Character Custom Inspector

The Soldier Custom Inspector

As you can see, the textures Foldout is only in the base class

Upvotes: 2

Views: 8981

Answers (1)

Louis Ingenthron
Louis Ingenthron

Reputation: 1368

When you override the Character editor, you don't ever call the base methods. You need to add base.OnEnable(); to Soldier editor's OnEnable method and base.OnInspectorGUI(); to Soldier editor's OnInspectorGUI method for them to utilize the functionality of the base class.

Also, a note just as a naming convention, anything deriving from Editor should be called Editor. So, for clarity's sake, these classes should be called CharacterEditor and SoldierEditor.

Upvotes: 2

Related Questions