- Home /
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
Answer by supernat · Jun 26, 2014 at 04:45 AM
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.
Thanks for the quick response.
I've tried wrapping the [DllImport(...)] statement in the following if statement:
#if UNITY_ANDROID ... #endif
I've also wrapped the single call to a function defined in the library in the same if statement and I still get the error when attempting to run the project in the Unity editor.
I'm confident that a runtime check of Application.platform would work for ensuring that the library is not loaded on the incorrect platform. However I want to clarify something:
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.
Does this suggest that there is no means, other than compiling the library in a different format, to run the project as-is; using the .so library file, in the Unity Editor?
Since, when using the free version of Unity, native plugins can only be used when building for mobile platforms, does this imply that this(running in the editor) can't be done?
Where do you have your .so placed? You should create a subfolder Android inside the plugins folder. That's a platform specific folder which isn't loaded on other platforms. See this wiki post
Sorry, you will also want to do a check if (!Application.isEditor) in your code. The value UNITY_ANDROID will be true for the project when Android is the build target.
Sorry for the delay in responding to your question @adlawren, I've updated my answer. But specifically, yes the free version won't run plugins. The bigger question is if you have the source code and what the complexity would be to build a similar windows library to offer the same functionality. Usually the Android so libraries are in the native C which has a lot of design constraints and peculiarities from a normal C compiler, and it has access to the Java runtime which you wouldn't have on Windows, so unless the library consists of a simplistic feature set, it may be expensive to convert it to windows. If you got the library from someone else, then it is not possible (well anything's possible I guess). You could see if that developer had a windows version though if that was the case.
Does this suggest that there is no means, other than compiling the library in a different format, to run the project as-is; using the .so library file, in the Unity Editor?
You can run native windows code on windows, not native AR$$anonymous$$ (or whatever android architecture compiled code) on windows....
Your answer
Follow this Question
Related Questions
How to display profiling data on screen (Android) 2 Answers
android shader issue 1 Answer
A node in a childnode? 1 Answer
Strange normal map on Android 0 Answers
Unity Script Editor Not Working 1 Answer