- Home /
The question is answered, right answer was accepted
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.
If your problem is that some instructions will fail to compile when you are in editor or building your game, I think you could use #if UNITY_EDITOR to make the compiler ignore the code after this statement (if you are not in editor).
http://docs.unity3d.com/$$anonymous$$anual/PlatformDependentCompilation.html
Updated question to point out why that won't work for me
Answer by meat5000 · Mar 15, 2015 at 11:46 PM
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.
That isn't really a solution is it? I don't want GetComponent
I want AddComponent
. And, as pointed out I can't use APIUpdaterRuntimeServices.AddComponent()
because my code is in a DLL and therefore can't be conditionally compiled out
This is whats in the manual under Unity 5 Upgrade $$anonymous$$isc info.
I don't know, perhaps they made an error in the text and meant AddComponent, not GetComponent, but its surely worth trying.
Updated answer with link to the blog. Worth reading.
Thanks! Yea, they mention AddComponent with Type.GetType. It sounds like that will work for my needs.
Check the re-edited answer. I literally just dropped the "" and it works the same :P
Your last example you added won't work for my case because when you try to export the app $$anonymous$$yEditorOnlyClass doesn't exist and so the code won't compile. That's why it's an "Editor Only" class. It's in an DLL sitting in Plugins/Editor hence I can't reference the type directly, only by string
Follow this Question
Related Questions
How to instantiate a MonoBehaviour from a downloaded DLL at runtime? 0 Answers
Metadata file Debug\Assembly-UnityScript-firstpass.dll' could not be found 4 Answers
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers
I need to access the handle of the window for a dll ... 1 Answer
UnityEditor namespace not found after importing dll 0 Answers