|
This may be a bug, or it could be I'm doing something wrong. I am having trouble getting the editor to recognize the existence of a C# class in an external DLL under certain conditions. Specifically, when this class derives from an intermediate class that resides in a different external DLL, which in turn derives from MonoBehavior. ie: Assembly A contains this class: class A : MonoBehavior Assembly B contains this class: class B : A The class A is recognized and shows up in the editor, but class B does not. When I move class B into the same assembly as class A, both of them ARE recognized and it all works fine. There seems to be something about them living in separate assemblies combined with the use of inheritance that is causing the editor not to recognize class B as a valid class deriving from MonoBehavior. Can anyone offer a solution/workaround? Is this a real bug? Thanks, Jeremy
(comments are locked)
|
|
Ok i've double checked it and it seems Unity doesn't recognise MonoBehaviour classes in this special case. I'm not sure why since with reflection you can see the class, so it is there but Unity doesn't include it in it's AssetDatabase so you can't use AddComponent. Well you can use AddComponent but it just prints a funny debug message: This happens because the class itself is there, but it's not registrated in the AssetDatabase. So i would say it's a bug (or limitation if you want) I would say that this was a bug personally.
Sep 01 '12 at 12:20 AM
numberkruncher
I got some news: I've done some tests with dynamically loaded assemblies (i think i've recompiled my dll 40+ times). It does work! The big problem: namespaces! We all know the docs states that namespaces aren't supported. Yes that's true, but when you compile an Assembly created in Visual studio it usually creates a namespace. Unity ignores the namespace when it imports the dll. Here is the flaw: It seems that the namespace is ignored, but infact it isn't. When you put the dll into the asset folder Unity "sees" the classes and can use it, but they are still in another namespace. When you create your two assemblies put all your classes in the global namespace, then it works ;) The same applies for Assemblies that are dynamically loaded (via WWW + reflection). The classes are there, but you can't use them unless they are in the global namespace.
Sep 28 '12 at 02:35 PM
Bunny83
I cannot get this working. Could you post a simple example of how you set up the classes? I had done a bunch of testing on this problem recently and I found several problems: I was never able to see the MonoBehaviour derrived classes in the project view (normally a DLL with MonoBehaviours has an arrow drop down that allows you to drag and drop them onto objects). However, I WAS able to add the components using scripting (AddComponent), BUT the monobehaviour it added to the object was completely broken. It showed up in the inspector as "Script" instead of a named class, and if you saved the scene/prefab and reloaded, it would lose the data and the link to the class completely because it did not store both a fileID and GUID in the .meta file for the object. I was even able to get the editor to crash every time when I went through certain steps involving prefabs. (See my post at http://forum.unity3d.com/threads/148617-Problems-compiling-DLLs-from-MonoDevelop?p=1022960&viewfull=1#post1022960) I reported this to Unity as a bug. I did also come up with a workaround that involves merging DLLs into one using Microsoft ILMerge. (See http://forum.unity3d.com/threads/148617-Problems-compiling-DLLs-from-MonoDevelop?p=1023568&viewfull=1#post1023568 for the workaround).
Sep 28 '12 at 06:54 PM
guavaman
Well, unfortunately I can't make it work in the editor ;) So it's an editor bug since i can use the components at runtime without any problem. The class type is there and can be used even from "normal" scripts. I was searching for a way to create the MonoScript sub assets manually, but that's not possible since all members of the MonoScript class map to native code. Also an imported assembly has no type (just UnityEngine.Object) in the editor. This makes it almost impossible to create a custom inspector for dll assemblies. So when you put both assemblies in the assetfolder you can just use AddComponent from another script. My test class is just called Class1 so i do this: And i get this: Note that the component doesn't have the correct name and no asset reference since there's no MonoScript asset for this class. It does work anyways but it's clearly an editor / importer bug.
Sep 28 '12 at 10:30 PM
Bunny83
Yes, this is exactly what I got adding it to a scene object with an editor script. Your serialized data will get trashed if you try to save/load it and the editor will crash if you put it on a prefab. Oh well. I already reported this as a bug to Unity so we'll see if they fix it. Thanks!
Sep 28 '12 at 10:38 PM
guavaman
(comments are locked)
|

Never tried such a setup, but it should work. Where have you placed your dlls? They should be just somewhere in the Assets folder.
I'll have to try this myself before i can say something concrete...
(As of Unity 3.5.6) Similar bug when I have a generic base compoennt class MyBaseComponent<T> : MonoBehaviour and a derived component MyDerivedComponent : MyBaseComponent<string>. This works in scripts, but not with a DLL (even if both classes are in the same DLL).