- Home /
Using dll's from C#?
Hello
I am trying to use a dll I made in C# .net 2.0 without any luck. From what I have read, the dll should be placed in the asset folder and can then be used as any other scripts.
My Dll:
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
public class SerialGPS_DLL2 : MonoBehaviour
{
public void SerialGPSInit(string comPort, int baudRate)
{
//Some code
}
public void SerialGPSClose()
{
//Some code
}
public void SerialGPSUpdate(ref double latitude, ref double longitude, ref string status)
{
//Some Code
}
}
How I use it: Adding dll as referance in Visual Studio(just to get intellisense).
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class SerialGPS : MonoBehaviour
{
private SerialGPS_DLL2 m_cSerialGPS_DLL;
void Start ()
{
m_cSerialGPS_DLL.SerialGPSInit("COM3", 4800);
}
void Update()
{
}
}
The error i get:
Internal compiler error. See the console log for more information. output was:
Unhandled Exception: Mono.CSharp.InternalErrorException: Assets/SerialGPS.cs(5,14): SerialGPS ---> Mono.CSharp.InternalErrorException: Assets/SerialGPS.cs(12,28): SerialGPS.m_cSerialGPS_DLL ---> System.TypeLoadException: Could not load type 'SerialGPS_DLL2' from assembly 'SerialGPS_DLL2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at (wrapper managed-to-native) System.Reflection.Assembly:InternalGetType (System.Reflection.Module,string,bool,bool)
at System.Reflection.Assembly.GetType (System.String name, Boolean throwOnError, Boolean ignoreCase) [0x00000] in <filename unknown>:0
at System.Reflection.Assembly.GetType (System.String name) [0x00000] in <filename unknown>:0
at Mono.CSharp.SimpleName.Error_TypeOrNamespaceNotFound (IMemberContext ec) [0x00000] in <filename unknown>:0
at Mono.CSharp.SimpleName.ResolveAsTypeStep (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0
at Mono.CSharp.Expression.ResolveAsBaseTerminal (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0
at Mono.CSharp.Expression.ResolveAsTypeTerminal (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0
at Mono.CSharp.MemberBase.ResolveMemberType () [0x00000] in <filename unknown>:0
at Mono.CSharp.MemberBase.Define () [0x00000] in <filename unknown>:0
at Mono.CSharp.Field.Define () [0x00000] in <filename unknown>:0
at Mono.CSharp.TypeContainer+MemberCoreArrayList.DefineContainerMembers () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at Mono.CSharp.TypeContainer+MemberCoreArrayList.DefineContainerMembers () [0x00000] in <filename unknown>:0
at Mono.CSharp.TypeContainer.DefineContainerMembers (Mono.CSharp.MemberCoreArrayList mcal) [0x00000] in <filename unknown>:0
at Mono.CSharp.Class.DefineContainerMembers (Mono.CSharp.MemberCoreArrayList list) [0x00000] in <filename unknown>:0
at Mono.CSharp.TypeContainer.DoDefineMembers () [0x00000] in <filename unknown>:0
at Mono.CSharp.Class.DoDefineMembers () [0x00000] in <filename unknown>:0
at Mono.CSharp.TypeContainer.Define () [0x00000] in <filename unknown>:0
at Mono.CSharp.ClassOrStruct.Define () [0x00000] in <filename unknown>:0
at Mono.CSharp.Class.Define () [0x00000] in <filename unknown>:0
at Mono.CSharp.RootContext.PopulateTypes () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at Mono.CSharp.RootContext.PopulateTypes () [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
My only theory at the moment is that I am building my dll in a way that is not supported.
The couple of first things I would try would be to put the DLL in the standard assets folder (as that gets compiled first), or maybe use c# 2.5 since that is the supported version. or maybe make your class public.
It should automatically be added to your solution if you've done it correctly.
Thanks for the reply.
I noticed the missing public in front of my class just before you replayed. Sadly it did not change the error I am getting.
$$anonymous$$oving the dll to standard assets did not change anything.
Building as .net 2.5 did not help.
I can see my dll in my project tree in Unity, but the Inspector does not display anything when I select it(not sure if it is suppose to displays anything).
Answer by evilrobot · May 21, 2012 at 05:25 PM
Yeah... lame... really... really... LAME error message. Just cost me several hours while I'm on a deadline. My .Net common libraries reference SYstem.IO.Ports... I need them to debug a serial peripheral device.
Answer by jeremyace · Nov 01, 2011 at 10:48 PM
You can drop .Net DLLs in your project, compiling order has no effect here as all Unity scripts are also compiled into .Net DLLs before the scene is run.
However, I think your issue comes from creating an instance of MonoBehaviour that way. Remove the MonoBehaviour subclass part from the SerialGPS_DLL2 class definition and try it.
Answer by Miivers · Nov 02, 2011 at 01:33 AM
I striped the dll for all functionality and found out it didn't like the System.IO.Ports namespace I was using.
After testing System.IO.Ports in a new class in Unity i got the error: "the type or namespace 'port' does not exist in namespace 'System.IO'".
So my problem does not seem to be dll related after all. Found a post about System.IO.Ports here.
Answer by shrek · Nov 02, 2011 at 05:23 AM
in the player settings change the .NET 2.0 subset to .NET2.0 it will work..
Your answer
Follow this Question
Related Questions
Using Native C++ dll in Unity 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers