Unity 广泛支持原生__插件__,即用 C、C++、Objective-C 等编写的原生代码库。插件允许游戏代码(用 C# 编写)调用这些库中的函数。此功能允许 Unity 与中间件库或现有的 C/C++ 游戏代码集成。
为了使用原生插件,首先需要使用基于 C 的语言编写函数来访问所需的任何功能并将它们编译到库中。在 Unity 中,还需要创建一个 C# 脚本来调用本机库中的函数。
原生插件应提供一个简单的 C 接口供 C# 脚本随后向其他用户脚本公开。当某些低级渲染事件发生时(例如,创建图形设备时),Unity 也可以调用原生插件导出的函数,请参阅原生插件接口页面以了解详细信息。
具有单个函数的非常简单的本机库可能具有如下所示的源代码:
float FooPluginFunction () { return 5.0F; }
要从 Unity 中访问此代码,可使用如下代码:
using UnityEngine;
using System.Runtime.InteropServices;
class SomeScript : MonoBehaviour {
#if UNITY_IPHONE
// 在 iOS 上,插件以静态方式链接到
//可执行文件中,因此我们必须使用 __Internal 作为
// 库名。
[DllImport ("__Internal")]
#else
// 其他平台会动态加载插件,因此
// 传递插件动态库的名称。
[DllImport ("PluginName")]
#endif
private static extern float FooPluginFunction ();
void Awake () {
// 在插件中调用 FooPluginFunction
// 并将 5 输出到控制台
print (FooPluginFunction ());
}
}
通常,插件是在目标平台上使用本机代码编译器构建的。由于插件函数使用基于 C 的调用接口,因此在使用 C++ 或 Objective-C 时必须避免名称错用问题。
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.