Created
March 21, 2016 20:43
-
-
Save simon-heinen/25ec5d90221c89dfbf10 to your computer and use it in GitHub Desktop.
A generic intent filter activtiy which can send intents into Unity. Uses a few tricks to stay generic and not specify any project specific names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bitstars.unity.intentfilter; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import com.unity3d.player.UnityPlayer; | |
public class IntentFilterActivity extends Activity { | |
private static final String LOG_TAG = IntentFilterActivity.class | |
.getSimpleName(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final String gameObjectName = "IntentFilter"; | |
final String methodName = "OnIntentFilter"; | |
final String messageToSend = getIntent().getData().toString(); | |
/* | |
* Get the main activity launch it and wait until the unity player is | |
* ready to receive the URL via UnityPlayer.UnitySendMessage() | |
*/ | |
String myOwnPackageName = getApplicationContext().getPackageName(); | |
Intent mainLauncherIntent = getPackageManager() | |
.getLaunchIntentForPackage(myOwnPackageName); | |
startActivity(mainLauncherIntent); | |
new AsyncTask<Void, Void, Void>() { | |
@Override | |
protected Void doInBackground(Void... params) { | |
Log.d(LOG_TAG, "Will now wait until unity player is ready.."); | |
while (UnityPlayer.currentActivity == null) { | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void result) { | |
Log.d(LOG_TAG, "Unity is ready, sending event: " | |
+ gameObjectName + "." + methodName + "(" | |
+ messageToSend + ")"); | |
UnityPlayer.UnitySendMessage(gameObjectName, methodName, | |
messageToSend); | |
} | |
}.execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment