Can a child class's variables show with the base class's Custom Editor without needing a child class Custom Editor?

If it helps at all, I am using Unity 2019.3.6f1.

I’ve created a custom Editor class for my script BaseClass so that I can mark some variables as read-only, embed messages within the inspector, and limit variable values to specific ranges. BaseClass was designed to have a lot of child classes, so I would like to use the tag [CustomEditor(typeof(BaseClass), true] to extend this functionality to all children. According to the CustomEditor documentation, the bool editorForChildClasses in the CustomEditor constructor lets all child classes inherit that base class editor functionality.

Some child classes will also need their own custom editor functionality, like keeping variable values in a certain range or showing warnings when a reference is not set. For these, I can make a custom editor extending from BaseClassEditor and have no issues. However, some of the child classes do not need a custom editor for their own variables. They have a variable or two that I’d like to show in the inspector but that doesn’t need the extra functionality, so I’d like to mark them with [SerializeField] and be done. But when I do this, the variable does not show at all because it is using the BaseClassEditor exclusively. This honestly feels like a bug because I cannot think of a reason to have the child class use the BaseClassEditor and hide its own properties by default. After all, I could use [HideInInspector] to hide the child class properties that should not appear in the inspector.

When editorForChildClasses is false, the simple child classes show all their properties. The issue then is, the BaseClassEditor functionality is no longer there, so it is possible to mess up the inspector settings. I could in theory make custom editors for all child classes, but since this system was designed to have literally hundreds of child classes of BaseClass, that is not realistically feasible. Ideally, the CustomEditor would have a setting out of the box to let me use the BaseClassEditor for child classes while still having the default inspector for the child class as well, but I do not know of such an option.

I was able to figure out the solution with help from PraetorBlue on the Unity forums! The discussion and my final solution can be found here!

First, I get a list of all the fields on this script.

FieldInfo[] childFields = target.GetType().GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  • DeclaredOnly filters out any fields from parent classes
  • Instance removes any static fields
  • Public and NonPublic make sure that I can get all the variables on the instance. The NonPublic flag is important because all of my variables are private or protected and use the [SerializeField] attribute.

Then in OnInspectorGUI(), I have the following after the base class fields:

if(!HasCustomInspector)
{
    foreach(FieldInfo field in childFields)
    {
        //if(field.IsNotSerialized || field.IsStatic)
        //{
        //    continue;
        //}
 
        if (field.IsPublic || field.GetCustomAttribute(typeof(SerializeField)) != null)
        {
           EditorGUILayout.PropertyField(serializedObject.FindProperty(field.Name));
        }
    }
}