分享

Building Plugins for iOS 为iOS创建插件

 3dC 2014-11-13

This page describes Native Code Plugins for the iOS platform.

本页描述在iOS平台的本地代码插件

Building an Application with a Native Plugin for iOS
为iOS创建一个带有本地插件的应用程序

  1. Define your extern method in the C# file as follows:
    在C#中定义如下外部方法:
    [DllImport ("__Internal")]
    private static extern float FooPluginFunction ();
  2. Set the editor to the iOS build target
    设置iOS编辑其的创建目标
  3. 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).
    将你的本地源代码文件添加到生成的XCode项目中的"Classes"文件夹中(但项目被更新后这个文件夹不会被覆盖,但是不要忘记备份你的本地代码)。

If you are using C++ (.cpp) or Objective-C (.mm) to implement the plugin you must ensure the functions are declared with C linkage to avoid name mangling issues.

如果你正在使用C++(.cpp) 或者Objective-C(.mm)来实现一个插件,那么你必须要确保你创建的功能使用C linkage方式进行声明,以避免出现名称重整问题

extern "C" {
  float FooPluginFunction ();
} 

Using Your Plugin from C#
使用C#创建插件

iOS native plugins can be called only when deployed on the actual device, so it is recommended to wrap all native code methods with an additional C# code layer. This code should check Application.platform and call native methods only when the app is running on the device; dummy values can be returned when the app runs in the Editor. See the Bonjour browser sample application for an example.

OS本地插件只有在被部署到实际的设备上时才能被调用,因此建议所有的本地代码的方法又有一个额外的C#代码层。此代码应该检查Application.platform和调用本地方法,但仅在应用程序在设备上运行时执行;应用程序在编辑器内运行时返回一个虚值。见Bonjour浏览器的一个简单的应用程序例子。

Calling C# / JavaScript back from native code
从本地代码调用C#或者JavaScript

Unity iOS supports limited native-to-managed callback functionality via UnitySendMessage:

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.

这个方法包含三个参数:目标游戏对象的名称,调用的脚本方法,传递给脚本方法的信息字符串。

Known limitations: 已知的限制:

  1. Only script methods that correspond to the following signature can be called from native code: function MethodName(message:string)
    只有符合以下命名规则的脚本方法才能被本地代码调用:function 方法名(信息:字符串类型)
  2. Calls to UnitySendMessage are asynchronous and have a delay of one frame.
    调用UnitySendMessage是异步的并且有一帧的延时。

Automated plugin integration 自动集成插件

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.

Unity iOS支持有限的插件自动集成方式。所有位于Asset/Plugings/iOS文件夹中后缀名为.a , .m , .mm , .c , .cpp的文件都将自动并入到已生成的Xcode项目中。然而,从Asset/Plugings/iOS合并完成到最终目的地可能会影响到部分的工作流程。后缀为.h的文件不能被包含在Xcode的项目树中,但他们将出现在目标文件系统中,从而使.m/.mm/.c/.cpp文件编译。

Note: subfolders are currently not supported.

注意:目前不支持子文件夹。

iOS Tips (iOS提示)

  1. Managed-to-unmanaged calls are quite processor intensive on iOS. Try to avoid calling multiple native methods per frame.
    在iOS中托管和非托管的调用是非常占用处理器的。尽量避免在一帧内调用多个本地方法。
  2. As mentioned above, wrap your native methods with an additional C# layer that calls native code on the device and returns dummy values in the Editor.
    如上所述,在编辑器中包装本地方法的外部的C#类调用设备上的本地代码将返回一个虚值。
  3. 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.
    从一个本地方法返回的字符串值应该是UTF-8编码的,并且分配在堆上。像这种字符串在Mono上进行编组调用是不受限制的。
  4. 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.
    如上所述,XCode项目中的Classes文件是一个非常好的保存本地代码的位置,因为他在项目更新后不会被覆盖。
  5. 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...".
    另一个用来存储本地代码的好位置是资源文件夹或者其子文件夹。在XCode项目中只需添加本地代码的应用即可:右键单击Classes文件夹的子文件夹然后选择"Add->Existing files..."

Examples 示例

Bonjour Browser Sample
Bonjour浏览器例子

A simple example of the use of a native code plugin can be found here

这里能够找到一个简单的使用本地代码的插件例子。

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.

这个示例演示了如果在Unity iOS程序中调用objective-C代码。这个程序实现了一个非常简单的Bonjour客户端。该程序包含一个iOS项目(Plugins/Bonjour.cs是一个针对本机代码的C#接口,而BonjourTest.js这是一个实现了程序逻辑的JS脚本)和内置在XCode项目中(Assets/Code)的本机代码。

页面最后更新:2011-11-01

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多