Unity3d-Plugin

原文地址: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

更多信息;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值