ここでは、デスクトッププラットフォーム (Windows/Mac OS X/Linux) 用の ネイティブコードのプラグイン について説明します。
Mac OSX では、プラグイン はバンドルとして展開されます。XCode を使ってバンドルプロジェクトを作成するには、File->NewProject… と移動し、Bundle -> Carbon/Cocoa Loadable Bundle (XCode 3 の場合)、または OS X -> Framework & Library -> Bundle (XCode 4 の場合) を選択します。
C++ (.cpp) か Objective-C++ (.mm) を使用してプラグインを実装する場合、名前マングリング の問題を避けるために、関数を C リンケージで宣言する必要があります。
extern "C" {
float FooPluginFunction ();
}
Windows のプラグインは、エクスポートされた関数を持つ DLL ファイルです。DLL ファイルを作成できる実際の言語や開発環境であればすべて、プラグインの作成に使用できます。 Mac OSX と同様に、C リンケージを持つ C++ 関数を宣言して、名前マングリングの問題を避ける必要があります。
Linux のプラグインは、エクスポートされた関数を持つ .so
ファイルです。これらのライブラリは、通常 C または C++ で書かれていますが、どの言語でも使用できます。
他のプラットフォームと同様に、名前マングリングの問題を避けるために、C リンケージを持つ C++ 関数を宣言する必要があります。
32 ビットや 64 ビットのプラグインを必要とする問題は、プラットフォームによって異なる方法で処理されます。
Windows と Linux では、プラグインは手動で管理することができます (例えば、64 ビットプレイヤーをビルドする前に、64 ビットライブラリを Assets/Plugins
フォルダーにコピーし、32 ビットプレイヤーをビルドする前には、32 ビットライブラリを Assets/Plugins
フォルダーにコピーします)。または、32 ビットバージョンのプラグインを Assets/Plugins/x86
に、64 ビットバージョンのプラグインを Assets/Plugins/x86_64
フォルダーに置きます。デフォルトでは、エディターはアーキテクチャ特有のサブディレクトリを最初に探します。そのディレクトリが存在しない場合は、ルートの Assets/Plugins
フォルダーからプラグインをコピーします。
ユニバーサル Linux ビルドでは、アーキテクチャ特有のサブディレクトリを使用する必要があることに注意してください (ユニバーサル Linux ビルドを作成するとき、エディターはルートの Assets/Plugins
フォルダーからプラグインをコピーしません)。
Mac OS X の場合、32 ビットと 64 ビットの両方のアーキテクチャを持つユニバーサルバイナリとしてプラグインをビルドする必要があります。
いったんビルドすると、バンドルは Unity プロジェクトの Assets->Plugins フォルダー (または、適当なアーキテクチャ特有のサブディレクトリ) に配置する必要があります。すると、Unity は以下の例のように、関数を定義したときの名前で探します。
[DllImport ("PluginName")]
private static extern float FooPluginFunction ();
PluginName にはライブラリのプレフィックスもファイル拡張子も含まないように注意してください。例えば、プラグインファイルの実際の名前は PluginName.dll (Windows) や libPluginName.so (Linux) となります。 プラグインのコードを変更する場合は、プロジェクト内のスクリプトを再コンパイルする必要があります。そうしないと、プラグインは最新のコンパイルされたコードを取得できません。
クロスプラットフォームのプラグインの場合は、.bundle (Mac)、.dll (Windows)、.so (Linux) ファイルを Plugins フォルダーに含める必要があります。 他の作業は必要ありません。Unity はターゲットプラットフォームに適切なプラグインを自動的に選択し、プレイヤーに加えます。
このプラグインプロジェクトは、非常に基本的な操作のみを実装します (数字の出力、文字列の出力、2 つの浮動小数点数の加算、2 つの整数の加算)。この例は、初めて Unity プラグインを作成するユーザーにとって参考になるかもしれません。 プロジェクトは ここ (Zip形式) からダウンロードでき、Windows、Mac、Linux のプロジェクトファイルが含まれています。
Unity のマルチスレッドレンダリングで動作するマルチプラットフォームプラグインの例は、低レベルのネイティブプラグインインターフェース を参照してください。
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.