This is a really small ‘not even a bug’ so I thought I’d post it here instead of doing a full blown bug report.
Here is the ‘problem’:
[CustomEditor(typeof(ResolutionDropdown))]
[CustomEditor(typeof(FullScreenModeDropdown))] // Not Allowed
public class ScreenUIEditor : Editor
{
...
}
Multiple CustomEditorAttributes aren’t allowed, but when I make my own attribute like this everything works perfectly:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] // Note AllowMultiple = true
public class CustomMultiEditor : CustomEditor
{
public CustomMultiEditor(Type inspectedType) : base(inspectedType) { }
public CustomMultiEditor(Type inspectedType, bool editorForChildClasses) : base(inspectedType, editorForChildClasses) { }
}
This is on Unity 2020.2.1f1 though so maybe it has already been taken care of, but otherwise I thought this would be a painless addition to make the Unity Editor betteror.
It feels semantically wrong for a custom editor to handle multiple types, which is probably why they set it up that way, and honestly I can’t imagine your code is very neat and tidy constantly evaluating what type you’re actually editing. Unless they’re child classes which they have an extra parameter for: Unity - Scripting API: CustomEditor.CustomEditor
Perhaps a custom property drawer is actually what you’re looking for, based on the fact both your example classes are “Dropdowns”?
Huh, those are some excellent points. My use case is kinda simple (just need to display a few messages and handle a few fields the same way) so I didn’t put much taught into it.
I have since actually just implemented these monobehaviours differently so I didn’t need the attribute anymore, but then I forgot this post
so I’ll mark it as resolved.
A custom editor has to actually handle the specific type it should be used for. Since you have two different classes it doesn’t make too much sense to use one editor for both. If they have some shared / common base class, you can write the editor for the base class. By passing “true” to the second argument of the CustomEditor attribute the same editor would be used for any derived classes unless it has its own specific custom editor.