このページでは、iOS プラットフォーム向けの ネイティブ コードプラグイン のビルド方法について説明します。
iOS のネイティブプラグインでアプリケーションをビルドするには、以下を行います。
呼び出したい各ネイティブ関数に対し、以下のように C# ファイルで extern メソッドを定義します。
[DllImport ("__Internal")]
private static extern float FooPluginFunction();
ネイティブコードのソースファイルを Unity プロジェクトに加えます。
Plugin Inspector ウィンドウでプラグインの設定をカスタマイズします。例えば、ネイティブコードが iOS 特有の場合は、プラグインが iOS に対してのみ有効であることを確認します。
ノート: C++ (.cpp) または Objective-C++ (.mm) を使用してプラグインを実装する場合は、名前マングリングの問題を回避するために、C リンケージを使用して関数を宣言する必要があります。
extern "C" {
float FooPluginFunction();
}
C または Objective-C で書かれたプラグインは、名前マングリングを使用しないため、これを使用する必要がありません。
iOS ネイティブプラグインは、実際のデバイスにデプロイされている場合にのみ呼び出すことができるため、すべてのネイティブコードメソッドを追加の C# コードレイヤーでラップする必要があります。このコードは、UNITY_IOS && !UNITY_EDITOR
条件付きコンパイルを使用するか、Application.platform
を確認し、アプリケーションがデバイスで実行されているときにのみネイティブメソッドを呼び出す必要があります。これを実装する簡単な方法は以下のとおりです。
void MyMethod()
{
# if UNITY_IOS && !UNITY_EDITOR
CallNativeMethodImplementation();
# else
CallEditorMethodImplementation();
# endif
}
For a more detailed implementation, download the Bonjour Browser sample below.
Unity iOS は、ネイティブからマネージへの限定的なコールバック機能をサポートします。これは、以下の 2 つの方法のいずれかで行うことができます。
UnitySendMessage
使用UnitySendMessage
を使用このオプションは簡単ですが、いくつかの制限があります。以下のようになります。
UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");
3 つのパラメーターがあります。
GameObject
の名前UnitySendMessage
使用には以下の制限があります。
void MethodName(string message);
UnitySendMessage
.これはより複雑なオプションです。デリゲートを使用する場合、C# 側のメソッドは静的で、MonoPInvokeCallback
属性でマークされています。メソッドをデリゲートとして extern メソッドに 渡す必要があります。extern メソッドは、関数としてネイティブコードに実装され、対応するシグネチャを持つ関数へのポインターを取ります。次に、ネイティブコードの関数ポインターが C# 静的方法に戻ります。
このメソッドの C# コードは以下のようになります。
delegate void MyFuncType();
[AOT.MonoPInvokeCallback(typeof(MyFuncType))]
static void MyFunction() { }
static extern void RegisterCallback(MyFuncType func);
コールバックを受け取る C コードは以下のようになります。
typedef void (*MyFuncType)();
void RegisterCallback(MyFuncType func) {}
Unity supports automated plug-in integration and copies all files with the following extensions to the generated Xcode project if you enable them for iOS in the Plugin Inspector window: .a
, .m
, .mm
, .c
, .cpp
, .h
. If any files with these extensions are located in the Assets/Plugins/iOS
folder, Unity only enables them for the iOS platform.
ノート: ファイルが生成された Xcode プロジェクトにコピーされた後、それらは Unity プロジェクトの対応するファイルにリンクされなくなります。Xcode でこれらのファイルを変更した場合は、逆に Unity プロジェクトにコピーする必要があります。そうでないと、Unity は次にプロジェクトをビルドするときにそれらを上書きします。
マネージコードからアンマネージコードの呼び出しは、iOS 上でプロセッサに高負荷を与えます。フレームごとに複数のネイティブメソッドを呼び出さないでください。
ネイティブメソッドを追加の C# レイヤーでラップします。このレイヤーはデバイス上でネイティブコードを呼び出し、エディターでダミーの値を返します。
ネイティブメソッドから返される文字列値は UTF–8 でエンコードされ、ヒープに割り当てられます。Mono マーシャリングは、このような文字列に対しては制限がありません。
ネイティブコードのプラグインを使用した簡単な例は Bonjour Browser Sample からダウンロードできます。
This example demonstrates how you can invoke Objective-C code from a Unity iOS application. This application implements a simple Bonjour client and consists of:
Plugins\Bonjour.cs
はネイティブコードへの C# インターフェースであり、BonjourTest.cs
はアプリケーションロジックを実装するスクリプトです。Assets/Plugins/iOS
に保存) - Automated plug-in Integration セクションで説明されているように、ビルドされた Xcode プロジェクトに追加する必要があります。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.