UnityPlayerでネイティブなダイアログを出すために。
幾つか有料無料のアセットがあるのですが、なんとなくこれをチョイス。
Unity 5での動作が不明なままなのがちょっと怖いですが、Assetストアにて評価・人気ともに高いため思い切ってみました(5.6.1f1で動作確認済)。
ひとまずiOSのみ動作確認し、手軽に使える感じでしたので、記事にしてみました。
いずれAndroidもやる予定です。
導入
ダウンロードページはここです。
無料アセットなので普通に選んでインストールしてしまえばOKです。
using TheNextFlow.UnityPlugins;
必要なusing文はこれだけです。
関数
iOS/Android共用のクラス MobileNativePopups と、iOS専用のクラス IosNativePopups 、Android専用のクラス AndroidNativePopups の三つがあり、それら全てに OpenAlertDialog関数があります。
AndroidNativePopupsクラスのみ、他にも関数があるようです。
// 1ボタンのダイアログ
public static void OpenAlertDialog(
string title, // タイトル部分の文字列
string message, // 本文の文字列
string cancel, // ボタンの文字列
Action onCancel // ボタン押下後のコールバック
);
ラムダ式で書けるのでちょっとした事で出すときも楽かと。サンプルから抜粋。
MobileNativePopups.OpenAlertDialog(
"Hello!",
"Welcome to Mobile Native Popups",
"Cancel",
() => { Debug.Log("Cancel was pressed"); }
);
MobileNativePopupsクラスの残り二つもとても単純です。引数が増えていくだけなので特に何も考えなくても問題無いかと思います。
// 2ボタン
public static void OpenAlertDialog(
string title,
string message,
string ok,
string cancel,
Action onOk,
Action onCancel
);
// 3ボタン
public static void OpenAlertDialog(
string title,
string message,
string ok,
string neutral,
string cancel,
Action onOk,
Action onNeutral,
Action onCancel
);
iOS専用で、4ボタン以上の処理も可能です。
public static void OpenAlertDialog(
string title,
string message,
string[] button_name,
CompletionHandler completion
);
使用するときはクラス名が違うのでご注意。
サンプルでは下記の様に記述していました。
# if UNITY_IOS
IosNativePopups.OpenAlertDialog(
"Hello!",
"Welcome to Mobile Native Popups",
"Cancel",
new String[] {
"First Button",
"Second Button",
"Third Button"
},
( buttonIndex ) => {
switch ( buttonIndex ) {
case 0:
Debug.Log( "Cancel was pressed" );
break;
case 1:
Debug.Log( "First button was pressed" );
break;
case 2:
Debug.Log( "Second button was pressed" );
break;
default:
Debug.Log( "Third button was pressed" );
break;
}
}
);
# endif