Google Play 结算插件扩展了 Unity 内置的应用内购买服务和资产(称为 Unity IAP),为你的游戏提供了 Google Play 结算库的所有最新功能。本指南说明了如何设置你的项目以使用此插件。本指南还介绍了如何在 Unity 游戏中使用 Google Play 结算库功能。
设置 Google Play 结算插件
要设置插件,请完成以下各个链接部分中的步骤
启用 Unity IAP 抽象层
Google Play 结算插件基于 Unity IAP 包含的抽象层构建,因此在下载和导入插件之前,你需要启用此抽象层。要启用 Unity IAP 抽象层,请执行以下操作
- 完成以下 Unity 教程中的所有步骤:为 Unity Services 设置你的项目。
- 完成以下 Unity 教程中的所有步骤:启用 Unity IAP 服务。
下载并导入插件
该插件以 .unitypackage
格式的 Unity 包形式提供。要下载并导入插件,请按照以下步骤操作
- 从代码库的 GitHub 发布页面下载最新版本的 Google Play Plugins for Unity。
在 Unity 菜单栏中,依次点击 Assets > Import Package > Custom Package。
找到你下载的
.unitypackage
文件并将其选中。在 Import Unity Package 对话框中,保持选中所有资源,然后点击 Import。
导入包后,你的项目资源中会添加一个名为 GooglePlayPlugins 的新文件夹(位于 Assets 文件夹的根目录)。此文件夹包含该插件的所有 Google Play 结算库资源。
配置构建设置
由于该插件扩展了 Unity IAP,除非从构建中移除 Unity IAP 中一些旧的、重叠的依赖项,否则 Unity 会遇到冲突并无法构建 Android APK。该插件提供了一种自动方式来从你的项目中移除冲突的库。要解决这些冲突,请按照以下步骤操作
在 Unity 菜单栏中,依次选择 Google > Play Billing > Build Settings。
在 Play Billing Build Settings 窗口中,点击 Fix。这将解决冲突并将冲突的 Unity IAP 文件移至备份目录。点击 Fix 后,按钮会变为 Restore,你可以点击该按钮来恢复原始的冲突文件。
启用插件
要启用该插件,请将 Unity IAP 的 Google Play 实现替换为 Google Play 结算插件。例如,在使用 Unity IAP 购买者脚本时,你需要将传递给 IAP 构建器的 StandardPurchaseModule
更改为使用 Google.Play.Billing.GooglePlayStoreModule
// Create a builder using the GooglePlayStoreModule.
var configurationBuilder =
ConfigurationBuilder.Instance(Google.Play.Billing.GooglePlayStoreModule.Instance());
如果你的游戏对多个平台使用相同的购买者脚本,那么你应该添加一个平台检查,以确保 Unity 将继续对其他平台使用其自己的 IAP 解决方案
ConfigurationBuilder builder;
if (Application.platform == RuntimePlatform.Android)
{
builder = ConfigurationBuilder.Instance(
Google.Play.Billing.GooglePlayStoreModule.Instance());
}
else
{
builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
}
如果你在 Google Play 商店之外的其他 Android 应用商店发布游戏,则仅在选择 Google Play 商店时替换默认的 Unity IAP 实现
ConfigurationBuilder builder;
if (Application.platform == RuntimePlatform.Android
&& SelectedAndoidAppStore == AppStore.GooglePlay)
{
builder = ConfigurationBuilder.Instance(
Google.Play.Billing.GooglePlayStoreModule.Instance());
}
else
{
builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
}
在游戏中实现 Google Play 结算库功能
Google Play 结算插件扩展了 Unity IAP 服务,因此你可以使用相同的 Unity API 来管理常见的购买工作流。请注意,由于 Google Play 结算库与 Unity 对其他应用商店的标准 IAP 实现之间存在差异,因此 API 行为会有一些细微的变化。如果你不熟悉 Unity IAP API,请参阅 Unity IAP 教程中的“制作购买脚本”部分,了解如何实现基本购买流程的示例。
Google Play 结算库还包含一些 Google Play 商店独有的功能。你可以通过扩展接口访问这些功能。本节的其余部分介绍了如何在你的游戏中实现这些独特的 Google Play 结算库功能。
启用延迟购买
Google Play 支持延迟购买(也称为待处理交易或待处理购买),用户可以在商店中使用现金创建购买并在以后完成。
要启用延迟购买,请使用 IAP 构建器通过调用 EnableDeferredPurchase()
方法修改模块的配置
// Create a builder using a GooglePlayStoreModule.
var configurationBuilder =
ConfigurationBuilder.Instance(Google.Play.Billing.GooglePlayStoreModule.Instance());
// Enable deferred purchases
configurationBuilder.Configure<Google.Play.Billing.IGooglePlayConfiguration>()
.EnableDeferredPurchase();
接下来,使用 Play 商店扩展程序实现延迟购买回调
// Get the plugin extensions for the Google Play Store.
_playStoreExtensions =
extensions.GetExtension<Google.Play.Billing.IGooglePlayStoreExtensions>();
// Set the deferred purchases callback.
_playStoreExtensions.SetDeferredPurchaseListener(
delegate(Product product)
{
// Do not grant the item here. Instead, record the purchase and remind
// the user to complete the transaction in the Play Store.
});
将模糊处理的帐号 ID 传递给 Google Play
你可以将模糊处理的用户帐号 ID 传递给 Google Play,以帮助检测滥用行为,例如检测是否有许多设备在短时间内在同一帐号上进行购买。
要传递模糊处理的帐号 ID,请调用扩展 API 中的 SetObfuscatedAccountId()
方法
// Get the plugin extensions for the Google Play Store.
_playStoreExtensions =
extensions.GetExtension<Google.Play.Billing.IGooglePlayStoreExtensions>();
// Pass an obfuscated account ID.
_playStoreExtensions.SetObfuscatedAccountId(obfuscatedAccountId);
将模糊处理的个人资料 ID 传递给 Google Play
你可以将模糊处理的个人资料 ID 传递给 Google Play,以帮助检测欺诈行为,例如检测是否有许多设备在短时间内在同一帐号上进行购买。这类似于传递模糊处理的用户帐号 ID。在这两种情况下,ID 都代表一个单一用户,但个人资料 ID 允许你在单一应用内的多个个人资料中唯一标识单一用户。将模糊处理的个人资料 ID 发送给 Google Play 后,你可以在购买收据中稍后检索该 ID。
要传递模糊处理的个人资料 ID,请使用 IAP 构建器通过调用 SetObfuscatedProfileId()
方法修改模块的配置
// Get the plugin extensions for the Google Play Store.
_playStoreExtensions =
extensions.GetExtension<Google.Play.Billing.IGooglePlayStoreExtensions>();
// Pass an obfuscated profile ID.
_playStoreExtensions.SetObfuscatedProfileId(obfuscatedProfileId);
确认订阅价格变更
Google Play 允许你更改有效订阅的价格。你的游戏用户必须在价格变更生效之前确认任何价格变更。要提示用户确认其订阅的价格变更,请调用 ConfirmSubscriptionPriceChange()
方法
// Get the plugin extensions for the Google Play Store.
_playStoreExtensions =
extensions.GetExtension<Google.Play.Billing.IGooglePlayStoreExtensions>();
_playStoreExtensions.ConfirmSubscriptionPriceChange(productId,
delegate (bool success)
{
// Returns whether the user has accepted the new price or not.
});
Unity API 行为变更
使用 Google Play 结算插件时,大多数 API 的行为与 Unity 对其他应用商店的标准 IAP 实现方式相同。但是,在某些情况下,API 的行为会有所不同。本节介绍了这些行为差异。
不支持开发者载荷
Google Play 已弃用开发者载荷,并正在用更具意义和上下文的替代方案取代它。因此,不支持开发者载荷。有关替代方案的更多信息,请参阅关于开发者载荷的页面。
你可以继续使用 Unity 对其他应用商店的标准 IAP 实现所定义的相同接口,包括 IStoreController
。发起购买时,你仍然可以使用 IStoreController
并调用 InitiatePurchase()
方法
public void InitiatePurchase(Purchasing.Product product, string payload);
但是,你传入的任何载荷都不会生效(不会显示在最终收据中)。
不支持 SubscriptionManager
Unity IAP 提供 SubscriptionManager
类来管理订阅。由于 Unity 对此类的标准 IAP 实现使用了开发者载荷,因此不支持此类。你仍然可以创建此类,但在使用类的任何 getter 方法时,你可能会收到不可靠的数据。
UpdateSubscription API 存在细微变化
Google Play 结算插件不支持使用 SubscriptionManager.UpdateSubscription()
和 SubscriptionManager.UpdateSubscriptionInGooglePlayStore()
方法来升级和降级你的订阅。如果你的游戏调用这些方法,将抛出 GooglePlayStoreUnsupportedException
。
Google Play 结算库提供了一个替代 API 来取代这些方法。要升级或降级订阅,请使用按比例收费模式调用 UpdateSubscription()
方法
void UpdateSubscription(Product oldProduct, Product newProduct,
GooglePlayStoreProrationMode prorationMode = GooglePlayStoreProrationMode.Unknown);
你可以在捕获 GooglePlayStoreUnsupportedException
时,用平台检查或 catch 块包装此方法调用。
有关如何使用按比例收费模式的更多信息和示例,请参阅设置按比例收费模式。