This page describes Native Code Plugins for the iOS platform.
本页描述在iOS平台的本地代码插件。
[DllImport ("__Internal")]private static extern float FooPluginFunction ();
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 ();}
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浏览器的一个简单的应用程序例子。
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: 已知的限制:
function MethodName(message:string)
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.
注意:目前不支持子文件夹。
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
联系客服