Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 16, 2015 at 07:38 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by greggman · Mar 15, 2015 at 11:13 PM · unityeditordlladdcomponent

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image raulrsd · Mar 15, 2015 at 11:23 PM 0
Share

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

avatar image greggman · Mar 15, 2015 at 11:29 PM 0
Share

Updated question to point out why that won't work for me

1 Reply

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image greggman · Mar 16, 2015 at 12:48 AM 0
Share

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

avatar image meat5000 ♦ · Mar 16, 2015 at 12:58 AM 0
Share

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.

avatar image greggman · Mar 16, 2015 at 01:17 AM 0
Share

Thanks! Yea, they mention AddComponent with Type.GetType. It sounds like that will work for my needs.

avatar image meat5000 ♦ · Mar 16, 2015 at 01:18 AM 0
Share

Check the re-edited answer. I literally just dropped the "" and it works the same :P

avatar image greggman · Mar 16, 2015 at 06:58 AM 1
Share

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

Show more comments

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges