UnityからAndroidのインテントを呼び出す

UnityからAndroidのメソッド(なんかおかしな言い方だが気にしない、、、)を呼び出すには、
AndroidJavaObject#Callが使えるのですが、
戻り値がプリミティブじゃない場合の呼び出し方で悩んだので記録しておきます。


Unity - Scripting API: AndroidJavaObject.Call


戻り値無しなら

AndroidJavaObject#Call("method name", args[]);


戻り値int型ならジェネリクスを使って

AndroidJavaObject#Call<int>("method name", args[]);

と言う感じで、ドキュメントにあります。


で、戻り値がプリミティブじゃないときは書いてなかったので、試行錯誤しました。

AndroidJavaObject.Call<AndroidJavaObject>("method name", args[]);

と言うふうに、AndroidJavaObjectを指定してやればいいようです。


解れば当然なのですが、、、


ということで、UnityからAndroidのSENDインテントを呼び出してみるコードはこんな感じ。

// Find the UnityPlayer and get the static current activity
AndroidJavaClass cUnityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject oCurrentActivity = cUnityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
		
// Get defenitions of Intent and it's constructor.
AndroidJavaObject oIntent = new AndroidJavaObject ("android.content.Intent");
// Call some methods
oIntent.Call<AndroidJavaObject> ("setAction", "android.intent.action.SEND");
oIntent.Call<AndroidJavaObject> ("setType", "text/plain");
oIntent.Call<AndroidJavaObject> ("putExtra", "android.intent.extra.TITLE", "Hello");
// Start the activity!
oCurrentActivity.Call ("startActivity", oIntent);

//Dispose them. Not sure if I need to do it or not...
oIntent.Dispose ();
oCurrentActivity.Dispose ();

Unityだけで完結できて、結構短いコードでもいけるので良い感じ。