本页面将介绍适用于桌面平台 (Windows/Mac OS X/Linux) 的原生代码插件。
在 Mac OSX 上,以捆绑包的形式部署插件。可使用 XCode 按以下方法创建捆绑包项目:选择 File > NewProject…,然后选择 Bundle > Carbon/Cocoa Loadable Bundle__(使用 XCode 3)或 OS X__ > Framework & Library > __Bundle__(使用 XCode 4)
如果使用 C++ (.cpp) 或 Objective-C (.mm) 来实现该插件,则必须确保使用 C 链接来声明函数以免发生名称错用问题。
extern "C" {
float FooPluginFunction ();
}
Windows 上的插件是具有导出函数的 DLL 文件。实际上,任何可创建 DLL 文件的语言或开发环境均可用于创建插件。 与 Mac OSX 一样,应使用 C 链接来声明所有 C++ 函数以免发生名称错用问题。
Linux 上的插件是具有导出函数的 .so
文件。这些库通常用 C 或 C++ 编写而成,但也可使用任何语言编写。
与其他平台一样,应使用 C 链接来声明所有 C++ 函数以免发生名称错用问题。
对于需要 32 位和/或 64 位插件的问题,处理方式因平台而异。
在 Windows 和 Linux 上,可以手动管理插件(例如,在构建 64 位播放器之前,将 64 位库复制到 Assets/Plugins
文件夹中,在构建 32 位播放器之前,将 32 位库复制到 Assets/Plugins
文件夹中),或者可以将 32 位版本的插件放在 Assets/Plugins/x86
中,将 64 位版本的插件放在 Assets/Plugins/x86_64
中。默认情况下,Editor 将首先查看特定于架构的子目录,如果该目录不存在,则从 Assets/Plugins
根文件夹中复制插件。
请注意,对于通用 Linux 构建,需要使用特定于架构的子目录(在进行通用 Linux 构建时,Editor 不会从 Assets/Plugins
根文件夹中复制任何插件)。
对于 Mac OS X,应将插件构建为包含 32 位和 64 位架构的通用二进制文件。
构建捆绑包后,应将其放在 Unity 项目的 Assets > Plugins 文件夹中(或适当的特定于架构的子目录中)。在 C# 脚本中定义如下所示的函数时,Unity 将按名称查找捆绑包:
[DllImport ("PluginName")]
private static extern float FooPluginFunction ();
请注意,__PluginName__ 不应包含库前缀和文件扩展名。例如,插件文件的实际名称在 Windows 上为 PluginName.dll,在 Linux 上为 libPluginName.so。 请注意,每次更改插件中的代码时,都需要在项目中重新编译脚本,否则插件将没有最新的编译代码。
对于跨平台插件,必须在 Plugins 文件夹中包含 .bundle(对于 Mac)、.dll(对于 Windows)和 .so(对于 Linux)文件。 然后便不需要您执行任何其他操作,Unity 会自动为目标平台选择正确的插件并将其包含在播放器中。
此插件项目只实现了一些非常基本的操作(打印一个数字,打印一个字符串,将两个浮点数相加,将两个整数相加)。如果这是您的第一个 Unity 插件,此示例可能会有所帮助。 此项目位于此处,其中包含 Windows、Mac 和 Linux 项目文件。
原生插件接口页面上提供了一个在 Unity 中使用多线程渲染技术的多平台插件示例。
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.