- Home /
How do you use a class from a .dll file?
I'm trying to use a class, "DevIO", from a Microchip library, "MCP2210-M-dotNet2.dll". When I downloaded the library, it had a managed and unmanaged folder, but Unity is reading both of them as native plugins. Supposedly, DllImport is used to access methods from a native plugin, but how do I access a class?
UPDATE: Previously, I did have "MCP2210-M-dotNet2.dll" in a Plugins folder. I added it to the Assets folder as suggested. I then had to manually edit the project references in MonoDevelop and browse to add it as a .Net Assembly. Now "using MCP2210" is recognized, and there are no errors in MonoDevelop, but Unity still has errors.
I read more into how to use the library, and it requires Microsoft Visual C++ 2010 Redistributable Package or the inclusion of "msvcp100.dll" and "msvcr100.dll". However, Unity says they aren't valid .Net Assemblies, so is there a way I can include the Microsoft Visual C++ Redistributable Package in Unity?
Answer by ThePunisher · Jul 26, 2017 at 07:01 PM
It looks like dealing with plugins is a Pro-only feature.
Now that I know more about the problem. You should be able to pull in the DLL's from the managed directory under the "MCP2210DLL-M-dotNet2" directory. You will need all of the DLL's under this directory inside your plugins directory.
The "MCP2210DLL-M-dotNet2.dll" and "msvcm90.dll" are both managed dlls written in c#. They will expose all the functionality from the other two dll's (msgvcp90.dll and msvcr90.dll) to you.
There's example documentation within the managed directory. Just open up the "MCP2210DLL-M_CSExampleCode.sln" solution. Within this solution is Program.cs which contains examples on how to use the code. All the code made available to you is under the MCP2210 namespace so your scripts will need the using MCP2210; directive. You won't need to add the DLLs as references to your unity solution since Unity will do this step automatically (assuming you have placed them in your plugins directory).
Edit: Once you've done everything above the following code in a c# script compiled just fine.
using UnityEngine;
using System.Collections;
using MCP2210;
public class MicrochipTest : MonoBehaviour
{
// Use this for initialization
void Start ()
{
//Variables
const uint MCP2210_VID = 0x04D8; // VID for Microchip Technology Inc.
const uint MCP2210_PID = 0x00DE; // PID for MCP2210
bool isConnected = false; // Connection status variable for MCP2210
MCP2210.DevIO UsbSpi = new DevIO(MCP2210_VID, MCP2210_PID);
}
// Update is called once per frame
void Update () {
}
}
As far as I understand it, he does not want to use a native plugin, but he want to access a .net assembly from Unity. This can be done simply by putting the .DLL in the assets folder of your project. Unity will take care of linking automatically and it should work as long as the DLL is compiled for .NET 3.5 or 4.6(experimental in unity).
Compiling c# code into a DLL and bringing it to Unity assets directory allows you to reference that code (classes, types and all). But it sounds like what he's been given is a native plugin. Something probably written in C, maybe c++.
This is what the Unity manual has to say about that:
"Unity has extensive support for native Plugins, which are libraries of native code written in C, C++, Objective-C, etc. Plugins allow your game code (written in Javascript or C#) to call functions from these libraries. This feature allows Unity to integrate with middleware libraries or existing C/C++ game code.
In order to use a native plugin you firstly need to write functions in a C-based language to access whatever features you need and compile them into a library. In Unity, you will also need to create a C# script which calls functions in the native library.
The native plugin should provide a simple C interface which the C# script then exposes to other user scripts. It is also possible for Unity to call functions exported by the native plugin when certain low-level rendering events happen (for example, when a graphics device is created), see the Native Plugin Interface page for details."
If what he has is a native plugin then he may have to build the Unity script which exploses the functionality built into the plugin.
If you read his post again, you'll see that it is not a native plugin he's talking about.
"$$anonymous$$CP2210-$$anonymous$$-dotNet2.dll".
Unity probably thinks its a native plugin because he's putting the file in a plugins-folder.
Your answer
Follow this Question
Related Questions
Referencing a DLL in C# 0 Answers
nonstatic extern functions from DLL plugin import 1 Answer
EntryPointNotFoundException - .so plugin (Linux) 0 Answers
How can I import and use a plugin/API? 0 Answers
Uniwii DLL found in editor but not in build version 3 Answers