2016-03-19 21 views
7

私は別のAndroidアプリケーションからUnity暗黙のインテントを使ってUnityアプリケーションを起動します。これはうまくいきますが、Unityで余分なデータをどのように読み込むのか分かりません。 UNITY APPを起動するAndroidアプリの起動時にAndroidのインテントの追加データを読む

アンドロイドINTENT

i=new Intent(); 
i.setAction("com.company.unityapp.MyMethod"); 
i.putExtra("KEY","This is the message string"); 
startActivity(i); 

UNITY APPのAndroidManifest.xml

<intent-filter> 
    <action android:name="com.company.unityapp.MyMethod" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

私は付属のスクリプトと私のシーンでゲームオブジェクトを持っています。 startメソッドの内部で、私はこれが動作していないと引数は常に空です意図

AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); 

AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent"); 
bool hasExtra = intent.Call<bool> ("hasExtra", "arguments"); 

if (hasExtra) { 
AndroidJavaObject extras = intent.Call<AndroidJavaObject> ("getExtras"); 
arguments = extras.Call<string> ("getString", "arguments"); 
} 

と一緒に渡された余分なデータを試し読みするには、このコードを持っています。どんな助けもありがとう。

答えて

9

これを理解するまでにかなりの時間がかかりました。オンラインで見つかったすべてのソリューションは、部分的に完了していました。以下は、カスタム暗黙のインテントを使用して、別のアンドロイドアプリケーションからUnityアプリケーションを起動するための完全なソリューションです。また、Unity内でインテントとともに送信された余分なデータにアクセスする方法もあります。

これを達成するには、インテントの追加データにアクセスするためにUnityによって使用されるAndroidプラグインを作成する必要があります。

アンドロイドPLUGIN:

public class MainActivity extends UnityPlayerActivity { 

    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     handleNewIntent(intent); 
    } 

    private void handleNewIntent(Intent intent){ 
     String text = intent.getStringExtra("KEY"); 
     UnityPlayer.UnitySendMessage("AccessManager","OnAccessToken", text); 
    } 
} 

/lib/classes.jarあなたはAndroidのプラグインフォルダにUnityのインストールフォルダからclasses.jarをコピーする必要があり


のAndroidManifest.xml

ここで重要なのは、使用されているパッケージ名です:com.company.plugin

ユニティ

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 
    sourceSets { 
     main { 
      java { 
       srcDir 'src/main/java' 
      } 
     } 
    }  
... 
... 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.2.1' 
    compile 'com.android.support:design:23.2.1' 
    compile files('libs/classes.jar') 
} 

//task to delete the old jar 
task deleteOldJar(type: Delete) { 
    delete 'release/AndroidPlugin.jar' 
} 

//task to export contents as jar 
task exportJar(type: Copy) { 
    from('build/intermediates/bundles/release/') 
    into('release/') 
    include('classes.jar') 
    ///Rename the jar 
    rename('classes.jar', 'AndroidPlugin.jar') 
} 

exportJar.dependsOn(deleteOldJar, build) 

コピーユニティ資産に作成AndroidPlugin.jarで使用されるの.jarを作成できるように

アプリのGradleのビルドファイルに次の行を追加します。

Gradleのファイルのビルド/プラグイン/ Androidの

UNITY APP:


プレーヤーにバンドル識別子を設定します。ここで重要な

がで用いたのと同じパッケージ名を使用することです

は、資産/プラグイン/アンドロイドでカスタムのAndroidManifest.xmlファイルを作成しますcom.company.plugin - 設定は、Androidのプラグインで設定と同じになるようにプラグイン。 また、インテント名にも注意してください。com.company.plugin.do

AndroidManifest。XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.plugin" 
     android:versionCode="1" android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="9" /> 
    <application android:label="@string/app_name"> 
     <activity android:name=".MainActivity" android:label="@string/app_name" 
      android:launchMode="singleTask" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="sensor"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="com.company.plugin.do" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="text/plain"/> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

ユニティスクリプトを作成し、シーン内のゲームオブジェクトにスクリプトを添付します。 OnAccessTokenは、アンドロイドプラグインから送信されたメッセージを受信し、インテントから送信された余分なデータを含むメソッドです。

public class accessManager : MonoBehaviour { 

    public void OnAccessToken(string accessToken) 
    { 
     Debug.Log("Message Received!!!! :" + accessToken); 
    } 
} 

Androidアプリ:

これはオーバーライドする必要はありませんソリューションであるユニティアプリケーションを起動して、意図余分なデータに

public void LaunchUnityApp(){ 
    Intent i=new Intent(); 
    i.setAction("com.company.plugin.do"); 
    i.setType("text/plain"); 
    i.putExtra("KEY","This is the text message sent from Android"); 
    startActivity(i); 
} 
+0

Hey Hardy、簡単な説明をありがとう。私は全体の手順に従った。 Native AppはUnity Appを起動しますが、メッセージを取得できません。私は間違って何をしているのだろうか? –

関連する問題