原文地址:http://docs.unity3d.com/Documentation/Manual/Plugins.html,因自己需要故翻译
Unity has extensive support for Plugins, which are libraries of native code written in C, C++, Objective-C, etc. Plugins allow your game code (written in Javascript, C# or Boo) to call functions from these libraries. This feature allows Unity to integrate with middleware libraries or existing C/C++ game code.
Unity广泛支持用C,C++或者Objective-C所写的插件。插件允许游戏代码(Javascript,C#或者Boo所写)调用插件库中的函数。这个特性允许Unity和中间件或者已有的C/C++游戏代码交互。
Note: On the desktop platforms, plugins are a pro-only feature. For security reasons, plugins are not usable with webplayers.
注意:在桌面平台插件式Pro版特性,因为安全原因,插件在webplayer上不能使用
In order to use a plugin you need to do two things:-
为了使用插件需要做如下两事
The plugin should provide a simple C interface which the C# script then exposes to other user scripts. It is also possible for Unity to call functions exported by the plugin when certain low-level rendering events happen (for example, when a graphics device is created), see the Native Plugin Interface page for details.
插件应该提供一个简单的C接口以让C#脚本暴露给其他脚本使用。Unity也可在底层特定渲染事件发生时(例如一个图像设备创建时)调用插件中的函数。细节请看Native Plugin Interface 。
Here is a very simple example:
这里有个简单的例子:
一个简单插件的C文件。
float FooPluginFunction () { return 5.0F; }
using UnityEngine; using System.Runtime.InteropServices; class SomeScript : MonoBehaviour { #if UNITY_IPHONE || UNITY_XBOX360 // On iOS and Xbox 360 plugins are statically linked into // the executable, so we have to use __Internal as the // library name.
//在Xbox360和ios上插件被静态的链接到可执行文件上,所以使用__Internal作为库名
[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 FooPluginFunction (); void Awake () { // Calls the FooPluginFunction inside the plugin // And prints 5 to the console print (FooPluginFunction ()); } }
Note that when using Javascript you will need to use the following syntax, where DLLName is the name of the plugin you have written, or "__Internal" if you are writing statically linked native code:
注意:使用gJavascript是将使用以下的语法。其中DLLName是动态库的名称或者__Internal
@DllImport (DLLName) static private function FooPluginFunction () : float {};
创建插件
In general, plugins are built with native code compilers on the target platform. Since plugin functions use a C-based call interface, you must avoid name mangling issues when using C++ or Objective-C.
通常插件使用原生代码在目标平台上编译而成。因为插件函数使用C调用接口,所以在使用C++或者Objective-C时要避免name mangling
For further details and examples, see the following pages:-
更多细节和示例请 查看以下网页
更多信息;