# ネイティブ UWP プラグインの呼び出しと実装

> Understand how to call and implement native plug-ins.

ネイティブ ユニバーサル Windows プラットフォーム (UWP) プラグインを呼び出して実装するには、Unity のネイティブプラグインの作成方法を理解している必要があります。ネイティブプラグインとその使用の詳細については、[ネイティブプラグイン](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native.md) を参照してください。

IL2CPP スクリプティングバックエンドは、ネイティブプラグインの P/Invoke メカニズムをサポートします。つまり、C# コードから直接ネイティブプラグインを呼び出すことができます。これを行うには、ネイティブ関数プロトタイプを指定してから呼び出します。

以下の例は、ネイティブプラグインを実装し、C# スクリプトから呼び出す方法を示しています。

1. Unity プロジェクトに新しい .cpp ファイルを作成し、以下のネイティブプラグインコードを挿入します。

   ```
       extern "C" __declspec(dllexport)
       int __stdcall CountLettersInString(wchar_t* str)
       {
           int length = 0;
           while (*str++ != nullptr)
               length++;
           return length;
       }
   ```

2. 新しい C# スクリプトを作成し、そのコンテンツを以下のコードと置き換えます。

   ```
   [DllImport("MyPlugin.dll")]
       private static extern int CountLettersInString([MarshalAs(UnmanagedType.LPWStr)]string str);

       private void Start()
       {
           Debug.Log(CountLettersInString("Hello, native plug-in!"));
       }
   ```

3. シーンのゲームオブジェクトにコンポーネントを追加し、再生モードを開始します。コンソールに 22 と表示されます。

   ```
   using UnityEngine;
   public class ExamplePlugin : MonoBehaviour
   {
       [DllImport("MyPlugin.dll")]
       private static extern int CountLettersInString([MarshalAs(UnmanagedType.LPWStr)]string str);

       private void Start()
       {
           Debug.Log(CountLettersInString("Hello, native plug-in!"));
       }
   }
   ```

## 追加リソース##additional-resources

* [ネイティブプラグイン](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native.md)
* [プラグインのインポートと設定](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/plug-in-inspector.md)
* [P/invoke の使用](/engine/6000.0/manual/platform-specific/windows-store/uwp-developing/uwp-il2cpp-scripting/uwp-il2cpp-plugins/uwp-pinvoke.md)
