Version: 2017.1
优化物理性能
Preparing your application for "In App Purchases"

构建适用于 iOS 的插件

本页面将介绍适用于 iOS 平台的原生代码插件

Building an Application with a Native Plugin for iOS

1.在 C# 文件中定义 extern 方法,如下所示:

[DllImport ("__Internal")]
private static extern float FooPluginFunction();
  1. Set the editor to the iOS build target
  2. Add your native code source files to the generated Xcode project’s “Classes” folder (this folder is not overwritten when the project is updated, but don’t forget to backup your native code).

如果使用 C++ (.cpp) 或 Objective-C++ (.mm) 来实现该插件,必须确保使用 C 链接来声明函数以免发生名称错用问题

extern "C" {
  float FooPluginFunction();
}

用 C 或 Objective-C 编写的插件不需要此声明,因为这些语言不存在名称错用。

Using Your Plugin from C#

iOS 原生插件只能在实际设备上部署时调用,因此建议使用额外的 C# 代码层封装所有本机代码方法。此代码应检查 Application.platform 并仅当应用程序在设备上运行时才调用本机方法;在 Editor 中运行应用程序时,可返回虚拟值。请参阅 Bonjour 浏览器示例应用程序来查看示例。

Calling C# / JavaScript back from native code

Unity iOS 通过 UnitySendMessage 支持有限的本机到托管回调功能:

UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");

This function has three parameters : the name of the target GameObject, the script method to call on that object and the message string to pass to the called method.

已知限制:

1.只能从本机代码调用与以下签名对应的脚本方法:function MethodName(message:string) 1.对 UnitySendMessage 的调用是异步的,并有一帧延迟。

自动插件集成

Unity iOS supports automated plugin integration in a limited way. All files with extensions .a,.m,.mm,.c,.cpp located in the Assets/Plugins/iOS folder will be merged into the generated Xcode project automatically. However, merging is done by symlinking files from Assets/Plugins/iOS to the final destination, which might affect some workflows. The .h files are not included in the Xcode project tree, but they appear on the destination file system, thus allowing compilation of .m/.mm/.c/.cpp files.

Note: subfolders are currently not supported.

iOS 提示

1.在 iOS 上,托管到非托管的调用是处理器密集型操作。尽量避免每帧调用多个本机方法。 1.如上所述,应使用额外的 C# 层封装您的本机方法,该层将调用设备上的本机代码并在 Editor 中返回虚拟值。 1. String values returned from a native method should be UTF–8 encoded and allocated on the heap. Mono marshaling calls are free for strings like this. 1. As mentioned above, the Xcode project’s “Classes” folder is a good place to store your native code because it is not overwritten when the project is updated. 1. Another good place for storing native code is the Assets folder or one of its subfolders. Just add references from the Xcode project to the native code files: right click on the “Classes” subfolder and choose “Add->Existing files…”.

示例

Bonjour 浏览器示例

可以在此处找到使用原生代码插件的简单示例

This sample demonstrates how objective-C code can be invoked from a Unity iOS application. This application implements a very simple Bonjour client. The application consists of a Unity iOS project (Plugins/Bonjour.cs is the C# interface to the native code, while BonjourTest.js is the JS script that implements the application logic) and native code (Assets/Code) that should be added to the built Xcode project.

优化物理性能
Preparing your application for "In App Purchases"