Is it possible to call a separately created C# assembly from Unity? I have an assembly I created for controlling a piece of hardware that was developed in C# using .NET 3.5 and I'd like to be able to call it from Unity. For example, when a users avatar collides with an object, I need to be able to call a function in the external library that will make the hardware do something. Is this possible? What is the easiest way to go about it?
Answer by Ricardo
Dec 08, 2009 at 07:20 PM
The problem is because you're using a .Net 3.5 assembly, whereas Unity ships with a version of Mono that is equivalent to .Net 2.0. While Unity supports external assemblies via Mono, your assembly has dependencies on a more recent version (System.Runtime.Serialization 3.0, judging from your comment).
Hmmm...so it's looking like my only option might be to figure out how to register by dll for COM interop and import it with a bunch of DllImport's? In other words, back to what Tetrad suggested!
Never tried that approach, to be frank. If you have the source for the DLL, you could rebuild it with .Net 2.0, but more convoluted approaches might be required if you only have the compiled library.
Actually, I do have the source, but I can't compile it to .NET 2.0 since it's using WCF and I'd have to work out another way to do that. Yes, it's layer upon layer of stuff going on which all seemed like a good idea at the time!
Which is 3.0 only. Ding!
Answer by Tetrad
Dec 08, 2009 at 05:28 PM
Have you read the documentation on plugin support?
Not especially helpful since it talks about plugins written in C, C++ or objective-C. I want to be able to instantiate an object of a type defined in my C# dll and use it. Since it's .NET, I thought it would be easier than using a bunch of DllImport's
Answer by Lance Sun
Dec 08, 2009 at 05:44 PM
Yeah. If it's a normal .NET assembly, just copy the .dll to anywhere in your Assets folder, and Unity will detect and use it. Also, when Unity generates the sln/csproj files, it's nice enough to adds the reference to that file for you too.
The problem is that when I added my dll it complained that Unity complained that it could load it because it doesn't have access to System.Runtime.Serialization. I tried adding that library as well, but it seems it can load it.
BCE0106: Failed to access the types defined in assembly 'System.Runtime.Serialization, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b77a5c561934e089'
I see. Sorry, i overlooked your mention of .NET 3.5 in your question.
Answer by Lance Sun
Dec 08, 2009 at 08:55 PM
I assume you don't have the source... in that case, this is a longshot, but if you only need DataContractSerializer and maybe a few other things from System.Runtime.Serialization, you can try grabbing the S.R.S assembly from the Moonlight 2.0 beta. C# 3.5 compiles for the 2.0 CLR, so it theoretically could work.
Answer by Matt 1
Dec 09, 2009 at 03:07 PM
Okay, so it looks like I'm pretty much screwed at the moment, which is disappointing. I was gravitating towards Unity precisely because I could use my C# skills and hoped it would be fairly straight forward to integrate with code I already have.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
No one has followed this question yet.
Will installing a newer mono version break Unity's mono installation ? (OSX)
Framework Version Selection
Standalone windows build giving mono: dll not found error
mono develop error
pdb2mdb usage error from command line