Creating a component without a type dependency

So I have a runtime MonoBehavior IN A PLUGIN that wants to see if a certain editor helper MonoBehavior is available FROM ANOTHER PLUGIN. It’s only available in the editor.

The solution was this

        object o = m_gameObject.AddComponent("MyEditorOnlyClass");
        if (o != null) {
            m_gameObject.SendMessage("InitEditorOnlyClass", this);
        }

But now adding a component by name is deprecated. I can’t use gameObject.AddComponent because it will fail to compile when not in the editor and the editor only class will also fail to compile for runtime only code (it accesses UnityEditor stuff).

So what’s the suggested workaround now that I’m not allowed to do this anymore?

The part about IN A PLUGIN means the code is in DLL so using #if UNITY_EDITOR won’t help because the code has already been compiled. MyEditorOnlyClass is in a DLL sitting in Plugins/Editor. The usage of MyEditorOnlyClass is in a DLL sitting in Plugins.

From the docs

AddComponent(string), when called with a variable cannot be automatically updated to the generic version AddComponent< T>(). In such cases the API Updater will replace the call with a call to APIUpdaterRuntimeServices.AddComponent(). This method is meant to allow you to test your game in editor mode (they do a best effort to try to resolve the type at runtime) but it is not meant to be used in production, so it is an error to build a game with calls to such method). On platforms that support Type.GetType(string) you can try to use GetComponent(Type.GetType(typeName)) as a workaround.

Here’s the Blog which includes some most useful help on alternatives.

OR, you could simply try

m_gameObject.AddComponent(MyEditorOnlyClass); //No " "

This works for me to add a script to an object.