文档

Unity Engine

Unity Engine

调用 iOS 的原生插件

Wrap native code in C# layer to only run code when using an iOS device.
阅读时间2 分钟最后更新于 1 个月前

您的应用仅在部署到真实设备时才能调用 iOS 原生插件。使用额外的 C# 代码层封装所有原生代码方法,确保仅在应用运行于设备时调用原生方法。将此 C# 文件存储在项目的
Assets
文件夹中。
此 C# 层可以使用平台条件编译或检查
Application.platform
。对于在 Unity 编辑器中运行的代码,应返回占位值。
请参考以下部分了解这些方法的简单实现。如需更详细的实现,请下载 Bonjour 浏览器示例。

使用条件编译

平台相关的编译比
Application.platform
更快,因为它是在编译时完成评估,而不是在运行时。
使用以下代码实现条件编译:
void MyMethod(){#if UNITY_IOS && !UNITY_EDITOR CallNativeMethodImplementation();#else CallEditorMethodImplementation();#endif}

检查 Application.platform

使用以下命令可在编辑器中实现
Application.platform
并返回占位值:
void MyMethod() { if (Application.platform != RuntimePlatform.OSXEditor) { return _GetLookupStatus(); } else { return "Done"; }}

其他资源