13

When someone clicks on a link in a webpage of form "com.foo.bar://testtest" I want it to open my unity game and for me to get the testtest data.

I'm an experienced programmer, but when it comes to android I kind of google my way around rather than really understanding anything. Bare that in mind. :)

I can react to links on android using intent-filters. However all the resources I've found have assumed you can extend your main activity to capture the new intent. It's possible to do that with unity, but for various reasons I'd rather not. I tried creating a new activity, exporting it to a jar, and adding this to my manifest in the application tag:

<activity android:name="com.foo.ProtocolCatcher"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
        <data android:scheme="com.foo.bar" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>

Clicking on a link successfully launches my game, but onto a black screen.

Edit: I've also tried this format to no change:

<activity android:name="com.foo.ProtocolCatcher"
          android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="com.foo.bar" />
  </intent-filter>
</activity>

What are the magic incantations to make the whole game boot, along with my custom activity, and let my custom activity read the incoming URL, without touching the main activity?

tenpn
  • 4,556
  • 5
  • 43
  • 63

1 Answers1

3

I suppose that you are missing a part of the boot sequence; the steps required are the following:

  1. Define the ProtocolCatcher Activity with te proper scheme (OK)
  2. Define the MainActivty, which represents your Unity3D game main Activity (OK)
  3. Start the MainActivity when the ProtocolCatcher Activity gets started (MISSING)

Implementing the third step is super easy; just edit your ProtocolCatcher Activity's onCreate() method:

//ProtocolCatcher
//...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...

    Intent gameIntent = new Intent(this, MainActivity.class);
    /*
        //Pass the extra data to the game if needed
        Intent sourceIntent = getIntent();
        Uri data = sourceIntent.getData();
        gameIntent.putExtra("uriData", data != null ? data.toString(): null); 
    */
    startActivity(gameIntent); //start the real game
    finish(); //close the ProtocolCatcher activity

}

Considering the fact that you are "injecting" the ProtocolCatcher Activity manually, if you have problem to refer MainActivity from the ProtocolCatcher onCreate() you can lookup the relative Class using reflection.

Community
  • 1
  • 1
bonnyz
  • 13,458
  • 5
  • 46
  • 70
  • Actually, I think that it is `UnityActivity` that represents Unity game, not `MainActivity`. – Max Yankov May 03 '15 at 11:43
  • Yes, I used `com.unity3d.player.UnityPlayerProxyActivity` as MainActivity. It worked. Only niggle is that it restarts the game if it's already open and you follow a link. – tenpn Oct 20 '15 at 11:09
  • ...I fixed the re-launch issue by wrapping the startActivity() in `if (com.unity3d.player.UnityPlayer.currentActivity == null)` – tenpn Oct 20 '15 at 11:30