Managed Plugins
데스크톱 플랫폼 플러그인 빌드

네이티브 플러그인

Unity has extensive support for native Plugins, which are libraries of native code written in C, C++, Objective-C, etc. Plugins allow your game code (written in Javascript or C#) to call functions from these libraries. This feature allows Unity to integrate with middleware libraries or existing C/C++ game code.

Note: For security reasons, plugins are not usable in web player.

In order to use a native plugin you firstly need to write functions in a C-based language to access whatever features you need and compile them into a library. In Unity, you will also need to create a C# script which calls functions in the native library.

The native 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 native plugin when certain low-level rendering events happen (for example, when a graphics device is created), see the Native Plugin Interface page for details.

A very simple native library with a single function might have source code that looks like this:

    float FooPluginFunction () { return 5.0F; } 

To access this code from within Unity, you could use code like the following:

    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.
       [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 ());
       }
    }

Javascript를 사용하고 있을 때 DLL의 이름(DLLName)이 plugin인 경우, 또는 “__Internal”에서 정적으로 링크된 네이티브 코드를 작성하는 경우에는, 다음 구문을 사용할 필요가 있습니다.

    @DllImport (DLLName)
    static private function FooPluginFunction () : float {};

네이티브 플러그인 생성

일반적으로 플러그인은 대상 플랫폼에서 네이티브 코드 컴파일러를 사용해 구축되어 있습니다. 플러그인 함수는 C 언어를 기반으로 콜 인터페이스(C-based call interface)를 사용하기 때문에 C++과 Objective-C를 사용하는 경우, 네임 맹글링(name mangling) 문제를 피해야 합니다.

그 밖의 정보

Managed Plugins
데스크톱 플랫폼 플러그인 빌드