【问题标题】:Include custom Activity inside of Unity Android Plugin (Without overriding UnityPlayerActivity)?在 Unity Android 插件中包含自定义 Activity(不覆盖 UnityPlayerActivity)?
【发布时间】:2013-09-07 07:48:05
【问题描述】:

我正在尝试为 Unity3D 编写一个 Android 插件以与 Google Play In App Billing 交互。我知道有现有的插件可以解决这个问题,但我希望自己做。

看来我需要捕获 Android 的 Activity::onActivityResult 才能通过 GPlay IAB SDK 成功购买。我的问题是我的 Java 类不包含 Activity,因为我希望它在实际 Unity 应用程序后面的后台运行。

这是来自 GPlay IAB SDK 的代码,用于启动购买流程。如果我将“UnityPlayer.currentActivity”作为活动传递,则弹出Google Play,您可以成功购买产品。但是,您不会收到到 OnIabPurchaseFinishedListener 的成功消息。如果购买不成功(即:您已经拥有该产品),那么我会在那里收到回调。

public void launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode,
                        OnIabPurchaseFinishedListener listener, String extraData)
...
...
act.startIntentSenderForResult(pendingIntent.getIntentSender(),
                                           requestCode, new Intent(),
                                           Integer.valueOf(0), Integer.valueOf(0),
                                           Integer.valueOf(0));

完整来源:http://pastebin.com/xwUbrwTz

这是来自 Google Play In App Billing SDK 的部分(示例代码),它发布了成功的回调(我没有收到)

/**
     * Handles an activity result that's part of the purchase flow in in-app billing. If you
     * are calling {@link #launchPurchaseFlow}, then you must call this method from your
     * Activity's {@link android.app.Activity@onActivityResult} method. This method
     * MUST be called from the UI thread of the Activity.
     *
     * @param requestCode The requestCode as you received it.
     * @param resultCode The resultCode as you received it.
     * @param data The data (Intent) as you received it.
     * @return Returns true if the result was related to a purchase flow and was handled;
     *     false if the result was not related to a purchase, in which case you should
     *     handle it normally.
     */
    public boolean handleActivityResult(int requestCode, int resultCode, Intent data)

完整来源:http://pastebin.com/VTfxJhKx

以下是 Google 的示例代码如何处理 onActivityResult 回调:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

        // Pass on the activity result to the helper for handling
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            // not handled, so handle it ourselves (here's where you'd
            // perform any handling of activity results not related to in-app
            // billing...
            super.onActivityResult(requestCode, resultCode, data);
        }
        else {
            Log.d(TAG, "onActivityResult handled by IABUtil.");
        }
    }

现在,我不想覆盖 UnityPlayerActivity 类的原因(在 Google 上搜索“扩展 UnityPlayerActivity Java 代码”,这是第二个链接。缺乏声誉使我无法发布直接链接。)是因为这需要您修改 androidmanifest.xml 以指向新的“Launcher” - 这是一个问题,因为一些现有的 Android 广告平台已经要求您修改启动器以指向它们自己的 Java 类。我希望能够与这些共存,这会阻止我扩展现有的 Unity Activity。

我试图在我的 Java 类中启动我自己的 Activity(它是由 Unity 毫无问题地生成的),但这会最小化 Unity 应用程序并拉出一个空白 Activity - 显然不是我想要的。

我的问题: 我可以扩展/捕获/挂钩到现有的 UnityAndroidPlayer 类并添加一个 onActivityResult 函数(默认情况下没有)。

如果不是,我可以在不关注手机的 Android 插件中进行活动吗?

如果不是,我可以修改 Google Play SDK (act.startIntentSenderForResult(..)) 中的代码,以不同的方式通知我吗?

如果没有,我该怎么办?

【问题讨论】:

  • 我正在尝试为 In-App Billing 编写一个插件并且有完全相同的问题。你能解决这个问题吗?我计划的方法是拥有自己的活动,但在我进行购买之前不启动它(并且在启动时具有透明背景,因此仅显示购买对话框)。所有其他初始化和调用将来自另一个非 Activity 类,我计划将 UnityPlayer.currentActivity 用于“上下文”。
  • 我无法解决它。我最终只是制作了一个自定义活动(我将其传递给了 Google Play 的示例代码),然后在 Android Manifest for Unity 中手动设置该活动以进行挖掘。看起来 Prime31 也无法解决这个问题,因为他的插件工作方式相同。
  • 您能否澄清一下您所说的“将其传递给 Google Play 的示例代码”以及“手动将活动设置为我的”是什么意思?对于隐藏 Activity 并显示游戏,你会怎么做?
  • 就我而言,我意识到我将在 Android 上显示的唯一 UI 是在购买过程中。这也是我需要一个 Activity 来处理 onActivityResult 的时候。所以我最常使用单例非活动类,并且只在购买调用期间启动一个活动。 Activity 在 Purchase 调用之后调用 finish()。
  • @Goat 我制作了一个自定义 Activity,它简单地扩展了默认的 UnityPlayerActivity,并将与 Google Play 相关的代码(来自他们的 SDK 示例)放入此 Activity。

标签: android-activity unity3d


【解决方案1】:

我能给出的最佳建议是使用导出构建选项(这将为您提供一个 Eclipse 工作区价值的项目,准备好运行)。 Unity 的活动只是代码 - 因此您可以将自己的代码挂接到其中,而无需对它们进行子类化。这确实意味着您将无法使用正常的 Build 和 Build and Run 构建(至少在您想要测试此特定功能时不会),但它会起作用。要在每次构建时自动执行该过程,您可以在 Unity 中编写构建后脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多