Call native plug-in for Android code
Call native plug-in code from C# scripts.
読み終わるまでの所要時間 1 分最終更新 9日前
The process to call code in native plug-ins for Android is the same as standard native plug-ins.
It's best practice to wrap all native plug-in method calls with an additional C# code layer that:
- Checks Application.platform and only calls native methods when the application is running on Android devices using the architecture that you compiled the native plug-in for. On other platforms and architectures, the additional C# code layer should return dummy values.
- Uses platform defines to control platform dependent code compilation and only compile code that uses the plug-in on platforms that have the plug-in available.
Sample package
The AndroidNativePlugin.unitypackage zip file contains a simple example of a native code plug-in distributed as a Unity package.
The sample shows how to invoke C++ code from a Unity application. The package includes a scene which displays the sum of two values as calculated by a native plug-in. To compile the plug-in, use Android NDK. For information on how to install Android NDK via the Unity Hub, see Android environment setup.
To install the sample:
- Download the zip file.
- Extract the file.
AndroidNativePlugin.unitypackage - In a Unity project, click Assets > Import Asset Package.
- In the Import Package file dialog, find and select the extracted file.
AndroidNativePlugin.unitypackage