Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by adlawren · Jun 26, 2014 at 04:16 AM · androideditorplugins

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adlawren · Jun 26, 2014 at 08:26 AM 0
Share

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?

avatar image Bunny83 · Jun 26, 2014 at 08:31 AM 1
Share

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

avatar image supernat · Jun 26, 2014 at 09:51 PM 1
Share

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.

avatar image supernat · Jul 01, 2014 at 07:33 PM 0
Share

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.

avatar image liortal · Jul 02, 2014 at 09:29 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges