Mat*_*man 6 unity-game-engine android-activity
我正在尝试为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));
Run Code Online (Sandbox Code Playgroud)
完整来源: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)
Run Code Online (Sandbox Code Playgroud)
完整来源: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.");
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我不想覆盖UnityPlayerActivity类的原因(在Google上搜索"扩展UnityPlayerActivity Java代码",它是第二个链接.缺乏声誉阻止我发布直接链接.)因为这需要您修改androidmanifest.xml指向新的"启动器" - 这是一个问题,因为Android的一些现有广告平台已经要求您修改启动器以指向他们自己的Java类.我希望能够与这些共存,这阻止我扩展现有的Unity活动.
我试图在我的Java类中启动我自己的Activity(它由Unity生成而没有问题)但是最小化了Unity应用程序并且提取了一个空白的Activity - 显然不是我想要的.
我的问题:我可以扩展/捕获/挂钩到现有的UnityAndroidPlayer类并添加一个onActivityResult函数(默认情况下它没有).
如果不是,我可以在不关注手机的Android插件中进行活动吗?
如果不是,我可以修改Google Play SDK中的代码(act.startIntentSenderForResult(..))以不同的方式通知我吗?
如果不是,我该怎么办?
我能给出的最好建议是使用 Export 构建选项(这将为您提供 Eclipse 工作区价值的项目,可供运行)。Unity 的活动只是代码 - 因此您可以将自己的代码挂接到其中,而无需对它们进行子类化。这确实意味着您将无法使用正常的“构建”和“构建并运行”构建(至少在您想要测试此特定功能时不能),但它会起作用。为了使每次构建过程自动化,您可以在 Unity 中编写构建后脚本。
| 归档时间: |
|
| 查看次数: |
2611 次 |
| 最近记录: |