如何在unity游戏中使用非主活动来获取自定义url ?

时间:2022-07-16 18:27:28

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.

当有人点击表单“com.foo”中的链接时。我想让它打开我的unity游戏,并让我获得testtest数据。

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. :)

我是一名经验丰富的程序员,但当谈到android时,我有点像谷歌,而不是真正理解任何东西。记住。:)

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:

我可以使用意图过滤器对android上的链接做出反应。然而,我发现的所有资源都假设您可以扩展您的主要活动来捕获新的意图。团结一致是可能的,但出于各种原因,我宁愿不这样做。我尝试创建一个新的活动,将它导出到jar中,并将其添加到应用程序标记中的清单中:

<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?

有什么魔法咒语可以让整个游戏启动,以及我的自定义活动,并让我的自定义活动读取传入的URL,而不触及主要活动?

1 个解决方案

#1


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. 使用te适当的方案定义协议捕捉活动(OK)
  3. Define the MainActivty, which represents your Unity3D game main Activity (OK)
  4. 定义MainActivty,它表示Unity3D游戏主活动(OK)
  5. Start the MainActivity when the ProtocolCatcher Activity gets started (MISSING)
  6. 启动ProtocolCatcher活动时启动主活动(缺失)

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

执行第三步非常简单;编辑您的协议捕捉活动的onCreate()方法:

//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.

考虑到您正在手动“注入”协议捕捉器活动,如果您有问题,可以从协议捕捉器onCreate()中引用MainActivity,您可以使用反射来查找相关类。

#1


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. 使用te适当的方案定义协议捕捉活动(OK)
  3. Define the MainActivty, which represents your Unity3D game main Activity (OK)
  4. 定义MainActivty,它表示Unity3D游戏主活动(OK)
  5. Start the MainActivity when the ProtocolCatcher Activity gets started (MISSING)
  6. 启动ProtocolCatcher活动时启动主活动(缺失)

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

执行第三步非常简单;编辑您的协议捕捉活动的onCreate()方法:

//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.

考虑到您正在手动“注入”协议捕捉器活动,如果您有问题,可以从协议捕捉器onCreate()中引用MainActivity,您可以使用反射来查找相关类。