2

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Louis_Ingenthron I have tried what you suggested, but got the same results... any idea?
Based on your edit, you did base.DrawDefaultInspector instead of base.OnInspectorGUI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.