Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use non-main activity to capture custom url in unity game?

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?

like image 690
tenpn Avatar asked Mar 19 '15 17:03

tenpn


1 Answers

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.

like image 76
bonnyz Avatar answered Nov 01 '22 01:11

bonnyz