Interop with other C# assemblies

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?

more ▼

asked Dec 08 '09 at 04:46 PM

Matt gravatar image

21 1 1 3

(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

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).

more ▼

answered Dec 08 '09 at 07:20 PM

Ricardo gravatar image

5.5k 20 33 98

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!

Dec 08 '09 at 07:57 PM Matt

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.

Dec 08 '09 at 08:04 PM Ricardo

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!

Dec 08 '09 at 09:01 PM Matt

Which is 3.0 only. Ding!

Dec 08 '09 at 09:04 PM Ricardo
(comments are locked)
10|3000 characters needed characters left

Have you read the documentation on plugin support?


more ▼

answered Dec 08 '09 at 05:28 PM

Tetrad gravatar image

7.8k 27 40 94

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

Dec 08 '09 at 06:38 PM Matt
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 08 '09 at 05:44 PM

Lance Sun gravatar image

Lance Sun
1.2k 14 18 42

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=, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Any ideas?

Dec 08 '09 at 06:10 PM Matt

I see. Sorry, i overlooked your mention of .NET 3.5 in your question.

Dec 08 '09 at 08:07 PM Lance Sun
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 08 '09 at 08:55 PM

Lance Sun gravatar image

Lance Sun
1.2k 14 18 42

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 09 '09 at 03:07 PM

Matt 1 gravatar image

Matt 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 08 '09 at 04:46 PM

Seen: 8809 times

Last Updated: Dec 08 '09 at 07:20 PM