本页面将介绍适用于 iOS 平台的原生代码插件。
1.在 C# 文件中定义 extern 方法,如下所示:
````
[DllImport ("__Internal")]
private static extern float FooPluginFunction();
````
1.将 Editor 设置为 iOS 构建目标。 1.将本机代码源文件添加到生成的 Xcode 项目的 Classes 文件夹中(更新项目时不会覆盖此文件夹,但不要忘记备份您的本机代码)。
如果使用 C++ (.cpp) 或 Objective-C++ (.mm) 来实现该插件,必须确保使用 C 链接来声明函数以免发生名称错用问题。
extern "C" {
float FooPluginFunction();
}
用 C 或 Objective-C 编写的插件不需要此声明,因为这些语言不存在名称错用。
iOS 原生插件只能在实际设备上部署时调用,因此建议使用额外的 C# 代码层封装所有本机代码方法。此代码应检查 Application.platform 并仅当应用程序在设备上运行时才调用本机方法;在 Editor 中运行应用程序时,可返回虚拟值。请参阅 Bonjour 浏览器示例应用程序来查看示例。
Unity iOS 通过 UnitySendMessage 支持有限的本机到托管回调功能:
UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");
此函数有三个参数:目标游戏对象的名称、要在该对象上调用的脚本方法以及要传递给被调用方法的消息字符串。
已知限制:
1.只能从本机代码调用与以下签名对应的脚本方法:function MethodName(message:string)
1.对 UnitySendMessage 的调用是异步的,并有一帧延迟。
Unity iOS 在一定程度上支持自动插件集成。位于 Assets\Plugins\iOS 文件夹中的扩展名为 .a、.m、.mm、.c、.cpp 的所有文件都将自动合并到生成的 Xcode 项目中。但是,合并是通过将 Assets\Plugins\iOS 中的文件符号链接到最终目标来完成的,而这可能会影响某些工作流程。.h 文件不包含在 Xcode 项目树中,但会出现在目标文件系统中,从而允许编译 .m/.mm/.c/.cpp 文件。
1.在 iOS 上,托管到非托管的调用是处理器密集型操作。尽量避免每帧调用多个本机方法。 1.如上所述,应使用额外的 C# 层封装您的本机方法,该层将调用设备上的本机代码并在 Editor 中返回虚拟值。 1.从本机方法返回的字符串值应该是 UTF–8 编码并在堆上分配。Mono 编组调用对于这样的字符串是无成本的。 1.如上所述,Xcode 项目的 Classes 文件夹是存储本机代码的好地方,因为在项目更新时不会覆盖此位置。 1.另一个存储本机代码的好地方是 Assets 文件夹或其子文件夹之一。只需将 Xcode 项目中的引用添加到本机代码文件:右键单击 Classes 子文件夹,然后选择 Add > Existing files。
可以在此处找到使用原生代码插件的简单示例
此示例演示如何从 Unity iOS 应用程序 调用 objective-C 代码。该应用程序实现了一个非常简单的 Bonjour 客户端。 该应用程序包含 Unity iOS 项目(_Plugins\Bonjour.cs_ 是本机代码的 C# 接口,而 BonjourTest.cs 是实现应用程序逻辑的脚本)和应该添加到构建的 Xcode 项目中的本机代码 (Assets\Code)。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.