Multiple custom editors for the same type
I'm making a plugin based C# application in Unity 5.6, where some plugins might sometimes be installed, sometimes not.
In my plugin Plugin1
, I have a component Plugin1.Component1
. In that same project, I implement a custom editor for it, called Plugin1.Component1Editor
, that is a [CustomEditor(typeof(Plugin1.Component1))]
. When Plugin1
is installed, the Component1
is available, and rendered with the custom editor.
My plugin Plugin2
depends on Plugin1
. Depending on settings in its component Plugin2.Component2
, it would like to alter the custom editor for Plugin1.Component1
.
I have implemented a new custom editor in Plugin2
, called Plugin2.Component1Editor
, that is also a [CustomEditor(typeof(Plugin1.Component1))]
. It inherits from UnityEditor.Editor
, not from Plugin1.Component1Editor
, as the latter caused problems with serialized properties not being found in Plugin1.Component1Editor
.
Plugin2.Component1Editor
won't collide with Plugin1.Component1Editor
at compile time, as it has its own namespace. But what actually happens in the Unity inspector?
The behaviour when I test it is the desired behaviour: The Plugin2.Component1Editor
renders the inspector, unless Plugin2
not installed. But why? I don't want to trust that it will keep doing so unless I know why.
Thank you!
EDIT: I was wrong, it wasn't rendering the Plugin2.Component1Editor
, it was the default Unity editor running. Neither of my custom editors are used. How can I specify which one I want to use?
Answer by helenaudd · Aug 23, 2017 at 06:17 AM
It seems that the solution was indeed inheritance.
I made the serialized properties handled by Plugin1.Component1Editor
and their retrieval protected instead of private, and then Plugin2.Component1Editor
inherits from Plugin1.Component1Editor
instead of UnityEditor.Editor
. Then the Plugin2.Component1Editor
became the rendering one, calling the Plugin1.Component1Editor
as needed.
The main part of the solution was that I made the OnEnable
call in Plugin1.Component1Editor
, which retrieves the serialized properties, protected, so that I could call it from Plugin2.Component1Editor
via base.OnEnable()
.
Thank you so much!! I had the same issue! i just couldn't figure out how i can get multiple custom editors. $$anonymous$$y problem was that [CustomEditor(typeof(Target))]
was still on the class where i inhered from. Therefor, my new custom editor class wasn't rendering. Again thanks buddy.