适用于桌面平台的插件是用 C、C++和 Objective C 编写的原生代码库。本页将介绍适用于 Windows、macOS 和 Linux 的插件。有关更多信息,请参阅原生插件的相关文档。
可以将 macOS 插件部署为捆绑包,如果使用 IL2CPP,则可以部署为松散的 C++ 文件(可以使用 [DllImport(“__Internal”)] 语法进行调用)。有关松散 C++ 插件的更多信息,请参阅适用于 IL2CPP 的 C++ 源代码插件的相关文档。
要使用 XCode 创建捆绑包项目,请打开 XCode 11,选择 File > New > Project__,然后导航到 macOS__ > Framework & Library > Bundle。有关使用 XCode 的更多信息,请参阅 Apple 关于 XCode 的文档。
必须将插件构建为包含 64 架构的通用二进制文件。或者,也可以提供单独的 dylib 文件。 如果使用 C++ (.cpp) 或 Objective-C (.mm) 来实现插件,则必须确保使用 C 链接来声明函数,从而避免名称错用问题。
extern "C"
{
float FooPluginFunction ();
}
Windows 上的插件是具有所导出函数的 .dll 文件,如果使用 IL2CPP,则是松散的 C++ 文件。可以使用大多数可以创建 .dll 文件的语言和开发环境来创建插件。必须使用 C 链接来声明任何 C++ 函数,从而避免名称错用问题。
Linux 上的插件是具有所导出函数的 .so 文件。虽然这些库通常采用 C 或 C++ 编写,但您也可使用任何语言编写。与其他平台一样,必须使用 C 链接来声明所有 C++ 函数以免发生名称错用问题。
在 Unity 中,__Plugin Inspector__ 负责管理插件。要访问 Plugin Inspector,请在 Project 窗口中选择一个插件文件。对于独立平台,可以选择与库兼容的 CPU 架构。对于跨平台插件,必须包含 .bundle 文件(对于 macOS)、.dll 文件(对于 Windows)和 .so 文件(对于 Linux)。Unity 会自动为目标平台选择正确的插件并将该插件包含在播放器中。请参阅有关 Plugin Inspector 的文档以了解更多信息。
构建捆绑包后,必须将这个包放在 Unity 项目的 Assets 文件夹中(或特定于架构的适当子目录中)。在 C# 脚本中定义如下所示的函数时,Unity 将按名称查找捆绑包:
[DllImport ("PluginName")]
private static extern float FooPluginFunction ();
__注意__:PluginName
不应包含库前缀和文件扩展名。例如,插件文件的实际名称在 Windows 上为 PluginName.dll,在 Linux 上为 libPluginName.so。
项目 | 描述 | 链接 |
---|---|---|
Simplest Plugin Example | 此项目实现了一些基本操作,比如打印一个数字,打印一个字符串,将两个浮点数相加,将两个整数相加。 | 点击 Unity GitHub 代码仓库的链接来下载 Simplest Plugin Example 项目。此项目包含 Windows、macOS 和 Linux 项目文件。 |
Native Renderer Plugin | 这是一个低级渲染插件的示例。此项目演示以下操作: - 在完成所有常规渲染后,从 C++ 代码渲染一个旋转的三角形。 - 从 C++ 代码填充程序化纹理(使用 Texture.GetNativeTexturePtr 来访问该纹理)。 |
点击提供的 Unity GitHub 代码仓库的链接来下载 Native Renderer Plugin。此项目包含 Windows、UWP、macOS、WebGL 和 Android 文件。 |
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.