Custom Inspector class and inheritance.

Hi guys.
I have 2 classes: the base class (which is abstract) and the derived one.
I also made 2 different editor scripts: one for the base and one for the derived.
Of course, the editor script for the derived class is derived from the editor script of the editor script of the base class (I think you lost me now…).

Basically I have something like:

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(AbstractBaseClass))]
public class AbstractBaseClassEditor : Editor {

    protected SerializedObject InspectorObjects;
    protected SerializedProperty InspectorObjectsIterator;

    void OnEnable() {
        InspectorObjects = new SerializedObject(target);
    }

    public override void OnInspectorGUI() {

        InspectorObjectsIterator = InspectorObjects.GetIterator();
        AbstractBaseClass abstractBaseClass = (AbstractBaseClass)target;

        EditorGUILayout.BeginVertical(); {
            bool next = InspectorObjectsIterator.NextVisible(true);
            while (next) {

                // do all I need to show the class as I want....

                if (next) {
                    EditorGUILayout.PropertyField(InspectorObjectsIterator);
                    next = InspectorObjectsIterator.NextVisible(true);
                }
            }
        } EditorGUILayout.EndVertical();
        InspectorObjects.ApplyModifiedProperties();
    }
}

and

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(DerivedClass), true)]
public class DerivedClassEditor : AbstractBaseClassEditor {

    public override void OnInspectorGUI() {

        base.OnInspectorGUI();

        InspectorObjectsIterator = InspectorObjects.GetIterator();
        DerivedClass derivedClass = (DerivedClass)target;

        EditorGUILayout.BeginVertical(); {
            bool next = InspectorObjectsIterator.NextVisible(true);
            while (next) {

                // do all I need to do with the additional derived class fields.

                if (next) {
                    EditorGUILayout.PropertyField(InspectorObjectsIterator);
                    next = InspectorObjectsIterator.NextVisible(true);
                }
            }
        } EditorGUILayout.EndVertical();
        InspectorObjects.ApplyModifiedProperties();
    }
}

Basically if I do this, in the inspector, I will have all the fields of both classes twice: the first time formatted as defined in the abstract base class editor script (so the base class fields formatted correctly and the derived class fields not formatted at all) and the second time as defined in the derived class editor script (so the base class fields not formatted at all and the derived class fields correctly formatted).
I’ve experimented a lot of combinations but I was not able to reproduce the wanted effect.

I’ve also checked on the forum but all the solutions proposed in the previous threads are not working for me and on top of that I really cannot see what I’m doing differently.

Can someone point me in the right direction?
Thanks

It is usually really helpful to write what the wanted effect is :wink:

If you don’t want the abstract visualization in the derived class, remove line 9 in the derived class. But I am not sure whether this is what you are looking for.

Hi Dantus.
Basically I’ve packed up a simple project as example.
If you look in the inspector, you can see the behaviour of the editor scripts described by me in the first post.

What I’m trying to achieve is to have the combined effect of the base class editor script and the derived class editor script: the name of the transform and the color of the transform field must be as defined in the base class editor script as well as the name of the gameobject and the color of the gameobject field defined in the derived class editor script.

Sorry if my idea sounds confusing but if you take a look to the example project, everything will became clear.

Thanks :slight_smile:

1705757–107335–Inspector Inheritance.zip (138 KB)

I see your issue… Since you loop in all SerializedProperty with an enumator, you draw the same stuff twice from the base type. Frankly, you don’t have many choices in this case;

  • You don’t use an enumator and you implement your serialized property by hand, like Unity does for their own type.
  • You do some name check in your derived editor so you don’t draw what the base one did. You could have a protected array of name the base editor would fill of all the fields it draws or something, and make the derived one skip those already drawn.
  • You click on Advanced Inspector in my signature, and never ever write a custom editor again. :stuck_out_tongue:

Your idea doesn’t sound confusing at all, you simply didn’t describe what you are trying to achieve :slight_smile:

LightStriker already gave you the answer. (Don’t use enumerator)

Thank you very very much to everyone !!!

This is my personal solution: the base class editor must not use the iterator and define manually every single field that must be drawn in a protected function that will be called by the children. The children editor class can use enumerator without any issue.

I’ve applied this solution to my REALLY complex and WONDERFUL scripts :stuck_out_tongue: and it seems to work as expected.

Hope this can help anyone with the same problem.

Cheers :slight_smile:

1705873–107344–Inspector Inheritance.zip (138 KB)