マネージプラグインとは、.NET アセンブリ を作成し、Visual Studio などのツールで Unity の外部でコンパイルして動的リンクライブラリ (DLL) にしたものです。
これは、ソースファイルとして Unity プロジェクトの Assets フォルダーに格納する標準的な C# スクリプトとは処理が異なります。Unity は標準的な C# スクリプトを変更するたびにコンパイルしますが、DLL は事前にコンパイルされているので変わりません。コンパイルされた .dll ファイルをプロジェクトに追加し、それが含むクラスを ゲームオブジェクト に、標準のスクリプトと同じ方法でアタッチすることが可能です。
C# のマネージドコードの詳細については、Microsoft のマネージコードに関するドキュメント を参照してください。
マネージプラグインには .NET コードのみが含まれているため、.NET ライブラリでサポートされていない機能にアクセスできません。ただし、マネージコードは Unity がスクリプトのコンパイルに使用する標準の .NET ツールにアクセスできます。
Unity で DLL を扱う場合、スクリプトを扱う場合よりも多くのステップを完了させる必要があります。ただし、代わりに .dll ファイルを作成して Unity プロジェクトに加えることが便利な場合もあります。以下はその例です。
このページでは、マネージプラグインを作成するための一般的なメソッドと、マネージプラグインの作成方法、Visual Studio を使用したデバッグセッションの設定方法について説明します。
マネージプラグインを作成するには、DLL を作成する必要があります。これを行うには、以下のような適切なコンパイラーが必要です。
.NET コードを生成するすべてのコンパイラーが Unity と互換性があるわけではありません。そのため、コンパイラーで重要な作業をする前に、利用可能ないくつかのコードでコンパイラーをテストする必要があります。DLL を作成するために使用する方法は、DLL に Unity API コードが含まれているかどうかによります。
C:\Program Files\Unity\Hub\Editor\<version-number>\Editor\Data\Managed\UnityEngine
に移動します。Unity.app
ファイルを探します。macOS の Unity DLL へのパスは、/Applications/Unity/Hub/Editor/<version-number>/Unity.app/Contents/Managed/UnityEngine
です。Unity.app
を右クリックします。UnityEngine
フォルダーには、多くのモジュールの .dll ファイルが含まれています。スクリプトでそれらを利用可能にするために、それらを参照します。名前空間によっては、Unity プロジェクトからコンパイルされたライブラリへの参照も必要です (例えば、UnityEngine.UI
)。プロジェクトフォルダーのディレクトリ ~\Library\ScriptAssemblies
でこれを探します。DLL に Unity の API コードが含まれていない場合、または Unity の DLL をすでに利用可能にしている場合、コンパイラーのドキュメントに従って .dll ファイルをコンパイルしてください。DLL をコンパイルするための正確なオプションは、使用するコンパイラーによります。例えば、Roslyn コンパイラーのコマンドライン csc
は、macOS では次のようになります。
csc /r:/Applications/Unity/Hub/Editor/<version-number>/Unity.app/Contents/Managed/UnityEngine.dll /target:library /out:MyManagedAssembly.dll /recurse:*.cs
この例で
/r
オプションで、ビルドに加えるライブラリへのパス (ここでは、UnityEngine
ライブラリ) を指定します。/target
オプションで、必要なビルドのタイプを指定します。“library” は DLL ビルドを意味します。/out
でライブラリ名を指定します。この場合、“MyManagedAssembly.dll” となります。/recurse
メソッドを使用して、現在の作業ディレクトリとサブフォルダーにある “.cs’” で終わるすべてのファイルを加えます。結果の .dll ファイルは、ソースファイルと同じフォルダーに表示されます。DLL をコンパイルしたら、他のアセットと同じように .dll ファイルを Unity プロジェクトにドラッグすることができます。すると、以下を行えます。
ここでは、以下を説明します。
DLLTest
という名前を使用)。MyUtilities
に名前変更します。using System;
using UnityEngine;
namespace DLLTest {
public class MyUtilities {
public int c;
public void AddValues(int a, int b) {
c = a + b;
}
public static int GenerateRandom(int min, int max) {
System.Random rand = new System.Random();
return rand.Next(min, max);
}
}
}
Unity で DLL のデバッグセッションを設定するには、以下を行います。
<project folder>/bin/Debug/DLLTest.dll
) を Assets フォルダーにコピーします。using UnityEngine;
using System.Collections;
using DLLTest;
public class Test : MonoBehaviour {
void Start () {
MyUtilities utils = new MyUtilities();
utils.AddValues(2, 3);
print("2 + 3 = " + utils.c);
}
void Update () {
print(MyUtilities.GenerateRandom(0, 100));
}
}
Unity は、DLL からのコードの出力をコンソールウィンドウに表示します。
Unsafe C# コード は、メモリに直接アクセスできるコードです。コンパイラーは、それがセキュリティリスクをもたらさないことを検証できないため、デフォルトでは有効になっていません。
以下の場合に unsafe コードを使う場合があります。
unsafe な C# コードのコンパイルに対応するには、Edit > Project Settings > Player > Other Settings の順に移動し、Allow Unsafe Code を有効にします。
詳細については、Microsoft のアンセーフコード、ポインター型、関数ポインター を参照してください。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.