Building Plugins for iOS(为iOS构建插件)
This page describes Native Code Plugins for the iOS platform.
这个页面描述iOS平台的本机代码的插件。
Building an Application with a Native Plugin for iOS
构建应用程序的本地插件iOS
- Define your extern method in the C# file as follows:
在c#文件中定义你的外面的方法如下:
[DllImport ("__Internal")]
private static extern float FooPluginFunction();
- Set the editor to the iOS build target
iOS设置编辑器来构建目标 - 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项目的“类”文件夹(这个文件夹不是覆盖当项目更新,但别忘了备份你的本机代码)。
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 + +(功能)来实现插件,您必须确保使用C链接声明的函数是为了避免名称改编问题。
extern "C" {
float FooPluginFunction();
}
Plugins written in C or Objective-C do not need this since these languages do not use name-mangling.
C或objective - C编写的插件不需要这个因为这些语言不使用名称改编。
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.
iOS本地插件可以只有当调用部署在实际的设备,所以建议将所有本地代码用额外的一层c#代码的方法。这段代码应该检查应用程序。平台和调用本地方法只有当应用程序运行在设备上,可以返回假值在应用程序运行时在编辑器中。看到你好浏览器示例应用程序的一个例子。
Calling C# / JavaScript back from native code
调用c# / JavaScript从本机代码
Unity iOS supports limited native-to-managed callback functionality via UnitySendMessage:
统一iOS支持通过UnitySendMessage native-to-managed回调功能限制:
UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");
UnitySendMessage(“GameObjectName1”、“MethodName1”、“信息发送”);
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.
这个函数有三个参数:目标GameObject的名称,脚本方法调用该对象和消息字符串传递给被调用的方法。
Known limitations:
已知的限制:
Only script methods that correspond to the following signature can be called from native code: function MethodName(message:string)
只有脚本方法对应于以下签名可以从本机代码调用:函数MethodName(信息:字符串)
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.
统一iOS支持自动插件集成在一个有限的方式。所有文件和扩展。m,功能,c,。cpp位于资产/插件/ iOS文件夹将被合并到Xcode项目自动生成。然而,合并是通过符号链接文件从资产/插件/ iOS到最终目的地,这可能会影响一些工作流。的。h文件不包括在Xcode项目树中,但它们出现在目标文件系统,从而允许编译。m /功能/ c /。cpp文件。
Note: subfolders are currently not supported.
注意:子文件夹目前不支持。
iOS Tips
iOS的技巧
Managed-to-unmanaged calls are quite processor intensive on iOS. Try to avoid calling multiple native methods per frame.
Managed-to-unmanaged调用处理器非常敏感,在iOS。尽量避免每帧调用多个本地方法。
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#调用本地代码在设备上并返回假值编辑器中。
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编组是免费的呼吁这样的字符串。
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项目的“类”文件夹是一个好地方来存储你的本机代码,因为它不是覆盖项目时更新。
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项目引用添加到本地代码文件:右键单击“类”子文件夹,选择“添加- >现有的文件…”。
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.
这个示例演示了如何objective - c代码从团结iOS应用程序可以调用。这个程序实现了一个非常简单的你好客户机。iOS应用程序由一个统一的项目(插件/你好。cs是c#接口本地代码,而BonjourTest。js是js脚本,实现了应用程序逻辑)和本地代码(资产/代码)应添加到构建Xcode项目。
1、Unity调用iOS:
1.1、在Unity C#中:
[ DllImport( "__Internal" )]
private static extern int _showSelectTitleDialog ( string title, string msg);
1.2、在Xcode Objective-C中:
extern "C" {
int _showSelectTitleDialog(const char *title, const char *msg) {
return [[UNDialogManager sharedManager]
showSelectDialog:[NSString stringWithUTF8String:title]
message:[NSString stringWithUTF8String:msg]];
}
}
2、在iOS中调用Unity:
2.1、在Xcode Objective-C中:
UnitySendMessage("DialogManager", "OnCancel", tag.UTF8String);
2.2、在Unity C#中:
public void OnCancel ( string idStr)
{
int id = int.Parse (idStr);
if (_delegates.ContainsKey (id)) {
_delegates [id] (-1);
_delegates.Remove (id);
Debug.LogWarning ( "===OnCancel idStr1:" + idStr);
} else {
Debug.LogWarning ( "===OnCancel idStr2:" + idStr);
}
}
参考资料:
http://docs.unity3d.com/Manual/PluginsForIOS.html