Using Native Plugins in an Android Project and Executing in the Unity Editor

Hello all!

I have something of a general question involving using native C/C++ plugins in a Unity Android project, using the free version of Unity.

I built a shared library in the “standard” fashion using the Android NDK and placed the “libMyPlugin.so” file in the Assets/Plugins/Android folder.

Upon trying to load the library via [DllImport(“MyPlugin”)] in a C# script in the assets folder, I receive the following error in the Unity Editor:

DllNotFoundException: MyPlugin

However, if I perform “build and run” in the editor, using an android device connected to my computer, I witness the expected behaviour in the app on my device.

It would seem that there may be some additional steps that are required to import the shared library within a script when the project is run in the Unity Editor.

Is anyone aware, having seen these errors, of a means to accomplish this? That is, in summation:

A means to import a shared library within a C# script, in an Android project using the free version of Unity, when running the game in the Unity Editor?

Development Setup:

Windows 8, 64 bit
Unity version: 4.5.1f3

The shared object is compiled for Android, not Windows. You would need an equivalent .dll library for the windows native plugin to run it in the editor. You should wrap the import statement with a test for the UNITY_ANDROID define. Also wrap any code that uses members of the library with a #if UNITY_ANDROID as well if those calls are for Android only. Lastly, if you want to run in the editor, you need to wrap the library calls also with an “if (!Application.isEditor)” check. This will bypass all of the function calls from the Android library since that library is a different architecture (ARM or MIPS usually, unless you’re using HAXM x86 but that requires an emulator) from your PC.

To allow calling those methods in your editor, you would need to duplicate the functionality in a Windows DLL named the same as the Android library, then remove all of the checks above. The Windows DLL would need to do the same thing that the Android SO is doing but will be compiled with the x86-based architecture which windows will understand.