Version: 2023.2
Language : English
Import a native plug-in for Android
Java and Kotlin source plug-ins

Call native plug-in for Android code

The process to call code in native plug-insA set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary
for Android is the same as standard native plug-insA platform-specific native code library that is created outside of Unity for use in Unity. Allows you can access features like OS calls and third-party code libraries that would otherwise not be available to Unity. More info
See in Glossary
.

Note: If you use individual C/C++ source files as plug-ins, use __Internal as the plug-in name in the DllImport attribute.

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 sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
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:

  1. Download the zip file.
  2. Extract the AndroidNativePlugin.unitypackage file.
  3. In a Unity project, click Assets > Import Package > Custom Package.
  4. In the Import Package file dialog, find and select the extracted AndroidNativePlugin.unitypackage file.
Import a native plug-in for Android
Java and Kotlin source plug-ins
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961