Version: 5.6
扩展 UnityPlayerActivity Java 代码
自定义 Android 启动画面

适用于 Android 的原生 (C++) 插件

Unity 支持用 C/C ++ 编写并封装在共享库 (.so) 中的 Android 原生插件。

要构建适用于 Android 的 C++ 插件,请使用 Android NDK 并熟悉构建共享库所需的步骤。

如果使用 C++ 来实现该插件,必须确保使用 C 链接来声明方法以免发生名称错用问题

extern "C" {
  float Foopluginmethod ();
}

构建库后,将输出的 .so 文件复制到 Unity 项目的 Assets/Plugins/Android 目录中。在 Inspector 中,将 .so 文件标记为与 Android 兼容,并在下拉框中设置所需的 CPU 架构:

Inspector 窗口中显示的原生 (C++) 插件导入设置
Inspector 窗口中显示的原生 (C++) 插件导入设置

要从 C# 脚本调用原生插件中的方法,请使用以下代码:

[DllImport ("pluginName")]
private static extern float Foopluginmethod();

Note that pluginName should not include the prefix (‘lib’) or the extension (‘.so’) of the filename. It is recommended to wrap all the native plug-in method calls with an additional C# code layer. This code checks Application.platform and calls native methods only when the app is running on the actual device; dummy values are returned from the C# code when running in the Editor. Use platform defines to control platform dependent code compilation.

原生 (C++) 插件示例

This zip archive contains a simple example of a native code plug-in. This sample demonstrates how C++ code is invoked from a Unity application. The package includes a scene which displays the sum of two values as calculated by the native plug-in. You will need the [Android NDK)(https://developer.android.com/ndk/index.html)to compile the plug-in.



扩展 UnityPlayerActivity Java 代码
自定义 Android 启动画面