Unity + DLL + MonoBehaviour
April 28th, 2010
Hi people!
Sorry for my absence but in last months I was engaged in my new job at Ubisoft (I’ll talk more later), I was hired as a C++ and Flash programmer here and started working hard since the beginning :)!
I’m finishing my first project here and will be releasing some news in 1 month from now!
Stay tunned!
But this post is to help people with Unity in matter that bothered me for almost a year!!! ![]()
There is a recurrent problem I constantly saw in forums and internet!
Using Monobehaviour based classes created inside a .dll in Unity throws a warning that the Component cannot be found/added!
The steps that causes this error are:
1 ) Create a DLL project in your editor.
2 ) Inside this project write a class that extends MonoBehaviour
3 ) Try to use it in UnityEditor
4 ) Unity throws “Can’t add ‘Class’ component because it doesn’t exists!”So the problem is that you need to externalize all implementation of your Class:Monobehaviour in order that it works!I found a nice workaround for this problem!
Follow this steps!
1 ) Create the DLL solution.
2 ) Create in the solution:
namespace com.your.namespace{ public class MyComponent : MonoBehaviour{}}
3 ) Generates the .dll (MyComponent now is in its assembly)
4 ) Go to UnityEditor, drop this dll in the “Plugins” folder as everybody knows.
5 ) Create a ‘MyComponent.cs’ file in this folder (or it can be anywhere).
6 ) In ‘MyComponent.cs’ of the UnityEditor write this one line:
public class MyComponent : com.your.namespace.MyComponent{};
7 ) Finished!!! Now every GameObject inside your .dll that call ‘AddComponent(”MyComponent”);’ will not cause the “Can’t add component because it doesn’t exists” error!”
This hack allow you to implements all your features that uses the MonoBehaviour class and hide it in the dll. In the UnityEditor only a “proxy” to the component name is made in order to all GameObjects could find it!
Any questions ask!




