Unity3d-Plugin

颛孙子民
2023-12-01

原文地址:http://docs.unity3d.com/Documentation/Manual/Plugins.html,因自己需要故翻译

插件 (Pro/Mobile版特性)

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:-

为了使用插件需要做如下两事

  • Write functions in a C-based language and compile them into a library.
  • Create a C# script which calls functions in the library.
  • 用C规则编写(主要是指在函数声明时可以使用extern c以避免name mangling)
  • 创建C#脚本以调用

     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 File of a Minimal Plugin:

一个简单插件的C文件。

float FooPluginFunction () { return 5.0F; }

C# Script that Uses the Plugin:

使用插件的C#脚本
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 {};

Creating a Plugin

创建插件

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:-

更多细节和示例请 查看以下网页

Further Information

更多信息;

 类似资料: