Unity3D :原生插件
推荐:将NSDT场景编辑器加入你的3D工具链
3D工具集:NSDT简石数字孪生
原生插件
Unity 支持原生插件,这些插件是可以用 C、C++ 和 Objective-C 等语言编写的原生代码库。插件允许用 C# 编写的代码调用这些库中的函数。此功能允许 Unity 与中间件库或现有 C/C++ 代码集成。
本机插件提供一个简单的 C 接口,然后 C# 脚本向其他脚本公开该接口。Unity 还可以调用本机插件在发生某些低级渲染事件时(例如,创建图形设备时)导出的函数。有关详细信息,请参阅低级本机插件接口。
有关本机插件的示例,请参阅本机渲染器插件。
使用本机插件
要使用本机插件:
- 使用基于 C 的语言编写函数以访问所需的功能。
- 将它们编译到库中。
- 在 Unity 中,创建一个调用本机库中函数的 C# 脚本。
您可以在目标平台上使用本机代码编译器构建本机插件。由于插件函数使用基于 C 的调用接口,因此必须使用 C 链接声明函数以避免名称重整问题。
示例
具有单个函数的简单本机库可能具有如下所示的代码:
float ExamplePluginFunction () { return 5.0F; }
要从 Unity 中访问此代码,请使用以下 C# 脚本:
using UnityEngine;
using System.Runtime.InteropServices;
class ExampleScript : MonoBehaviour {
#if UNITY_IPHONE
// On iOS plugins are statically linked into
// the executable, so we have to use __Internal as the
// library name.
[DllImport ("__Internal")]
#else
// Other platforms load plugins dynamically, so pass the
// name of the plugin's dynamic library.
[DllImport ("PluginName")]
#endif
private static extern float ExamplePluginFunction ();
void Awake () {
// Calls the ExamplePluginFunction inside the plugin
// And prints 5 to the console
print (ExamplePluginFunction ());
}
}
由3D建模学习工作室整理翻译,转载请注明出处!