我正在尝试为 Unity3D 编写一个 Android 插件来与 Google Play In App Billing 进行交互。我知道有可用的插件,但我希望自己做。

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

这是来自 GPlay IAB SDK 的代码,用于启动购买流程。如果我通过“UnityPlayer.currentActivity”作为 Activity ,就会弹出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 上搜索“Extending the UnityPlayerActivity Java Code”,这是第二个链接。缺乏声誉使我无法发布直接链接。)是因为这需要您进行修改androidmanifest.xml 指向新的“启动器” - 这是一个问题,因为一些现有的 Android 广告平台已经要求您修改启动器以指向它们自己的 Java 类。我希望能够与这些共存,这会阻止我扩展现有的 Unity Activity。

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

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

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

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

如果没有,我该怎么办?

请您参考如下方法:

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


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!