在 Unity 項(xiàng)目中使用 Google Play 結(jié)算庫(kù)

來(lái)源: Google Developers
作者:Google Developers
時(shí)間:2021-01-06
18373
Google Play結(jié)算服務(wù)插件擴(kuò)展了Unity的應(yīng)用內(nèi)購(gòu)買(mǎi)內(nèi)置服務(wù)和資產(chǎn)(名為Unity IAP),可以為您的游戲提供Google Play結(jié)算庫(kù)的所有最新功能。本指南介紹了如何設(shè)置您的項(xiàng)目以使用此插件,還介紹了如何在通過(guò)Unity開(kāi)發(fā)的游戲中實(shí)現(xiàn)結(jié)算庫(kù)功能。

Google Play結(jié)算服務(wù)插件擴(kuò)展了Unity的應(yīng)用內(nèi)購(gòu)買(mǎi)內(nèi)置服務(wù)和資產(chǎn)(名為Unity IAP),可以為您的游戲提供Google Play結(jié)算庫(kù)的所有最新功能。本指南介紹了如何設(shè)置您的項(xiàng)目以使用此插件,還介紹了如何在通過(guò)Unity開(kāi)發(fā)的游戲中實(shí)現(xiàn)結(jié)算庫(kù)功能。

設(shè)置Google Play結(jié)算服務(wù)插件

如需設(shè)置此插件,請(qǐng)完成以下每個(gè)鏈接部分中的步驟:

啟用Unity IAP抽象層。

下載并導(dǎo)入插件。

配置插件的構(gòu)建設(shè)置。

啟用插件。

啟用Unity IAP抽象層

Google Play結(jié)算服務(wù)插件基于Unity IAP中自帶的抽象層,因此您需要啟用此抽象層后才能下載并導(dǎo)入該插件。如需啟用Unity IAP抽象層,請(qǐng)執(zhí)行以下操作:

完成以下Unity教程中的所有步驟:針對(duì)Unity服務(wù)設(shè)置項(xiàng)目。

完成以下Unity教程中的所有步驟:?jiǎn)⒂肬nity IAP服務(wù)。

下載并導(dǎo)入插件

插件將作為.unitypackage格式的Unity軟件包提供。如需下載并導(dǎo)入插件,請(qǐng)按以下步驟操作:

從代碼庫(kù)的GitHub版本頁(yè)面中下載適用于Unity的最新版Google Play插件。

在Unity菜單欄中,依次點(diǎn)擊Assets>Import Package>Custom Package。

找到.unitypackage文件的下載位置并選擇該文件。

在Import Unity Package對(duì)話框中,選擇所有資產(chǎn)并點(diǎn)擊Import。

軟件包導(dǎo)入后,系統(tǒng)會(huì)在項(xiàng)目的資產(chǎn)中添加一個(gè)名為GooglePlayPlugins的新文件夾(位于Assets文件夾的根目錄下)。此文件夾包含插件的所有結(jié)算庫(kù)資產(chǎn)。

配置構(gòu)建設(shè)置

由于插件擴(kuò)展了Unity IAP,因此除非從build中移除Unity IAP中一些較舊的重疊依賴項(xiàng),否則Unity會(huì)遇到?jīng)_突且無(wú)法構(gòu)建Android APK。插件提供了一種從項(xiàng)目中自動(dòng)移除沖突庫(kù)的方法。如需解決這些沖突,請(qǐng)按以下步驟操作:

從Unity菜單欄中依次選擇Google>Play Billing>Build Settings。

在“Play Billing Build Settings”窗口中,點(diǎn)擊Fix。這樣就可以解決沖突并將沖突的Unity IAP文件移至備份目錄。點(diǎn)擊Fix后,該按鈕會(huì)變成Restore,點(diǎn)擊后可恢復(fù)原始的沖突文件。

啟用插件

如需啟用插件,請(qǐng)將Google Play的Unity IAP實(shí)現(xiàn)替換為Google Play結(jié)算服務(wù)插件。例如,使用Unity IAP購(gòu)買(mǎi)者腳本時(shí),您要更改傳遞到IAP構(gòu)建器中的StandardPurchaseModule以使用Google.Play.Billing.GooglePlayStoreModule:

//Create a builder using the GooglePlayStoreModule.

var configurationBuilder=

ConfigurationBuilder.Instance(Google.Play.Billing.GooglePlayStoreModule.Instance());

注意:為了避免與Unity的標(biāo)準(zhǔn)IAP實(shí)現(xiàn)發(fā)生沖突,每當(dāng)代碼引用Google Play結(jié)算服務(wù)插件中的公共類時(shí),您都應(yīng)該通過(guò)包含Google.Play.Billing完整地指定命名空間。本指南中的所有代碼示例均采用此方法。

如果您的游戲?qū)⑼粋€(gè)購(gòu)買(mǎi)者腳本用于多個(gè)平臺(tái),應(yīng)添加一項(xiàng)平臺(tái)檢查,確保Unity針對(duì)其他平臺(tái)繼續(xù)使用自己的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應(yīng)用商店中發(fā)布游戲,則只有在選擇Google Play商店時(shí)才應(yīng)替換默認(rèn)的Unity IAP實(shí)現(xiàn):

ConfigurationBuilder builder;

if(Application.platform==RuntimePlatform.Android

&&SelectedAndoidAppStore==AppStore.GooglePlay)

{

builder=ConfigurationBuilder.Instance(

Google.Play.Billing.GooglePlayStoreModule.Instance());

}

else

{

builder=ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

}

在游戲中實(shí)現(xiàn)Google Play結(jié)算庫(kù)功能

Google Play結(jié)算服務(wù)插件擴(kuò)展了Unity IAP服務(wù),因此您可以使用相同的Unity API管理通用的購(gòu)買(mǎi)流程。請(qǐng)注意,由于結(jié)算庫(kù)與其他應(yīng)用商店的Unity標(biāo)準(zhǔn)IAP實(shí)現(xiàn)之間存在差異,因此API行為也發(fā)生了一些細(xì)微變化。如果您是Unity IAP API新手,請(qǐng)參閱Unity IAP教程中的“Making a Purchase Script”部分,通過(guò)示例了解如何實(shí)現(xiàn)基本購(gòu)買(mǎi)流程。

結(jié)算庫(kù)還包括一些Google Play商店獨(dú)有的功能。您可以通過(guò)擴(kuò)展接口訪問(wèn)這些功能。本部分的其余內(nèi)容介紹了如何在游戲中實(shí)現(xiàn)這些獨(dú)有的結(jié)算庫(kù)功能。

啟用推遲購(gòu)買(mǎi)功能

Google Play支持推遲購(gòu)買(mǎi)(也稱為待處理的交易或待處理的購(gòu)買(mǎi)交易),在這種情況下,用戶可以創(chuàng)建購(gòu)買(mǎi)交易并稍后在實(shí)體店中使用現(xiàn)金完成購(gòu)買(mǎi)交易。

如需啟用推遲購(gòu)買(mǎi)功能,請(qǐng)?jiān)贗AP構(gòu)建器中調(diào)用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();

接下來(lái),使用Play商店擴(kuò)展程序?qū)崿F(xiàn)推遲購(gòu)買(mǎi)回調(diào):

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

});

向Google Play傳遞經(jīng)過(guò)混淆處理的帳號(hào)ID

您可以向Google Play傳遞經(jīng)過(guò)混淆處理的用戶帳號(hào)ID以方便檢測(cè)濫用行為,例如檢測(cè)是否有大量設(shè)備在短時(shí)間內(nèi)使用同一帳號(hào)進(jìn)行購(gòu)買(mǎi)。

注意:此帳號(hào)ID也會(huì)與購(gòu)買(mǎi)數(shù)據(jù)一起返回。如果您使用開(kāi)發(fā)者載荷識(shí)別用戶,則可以使用此帳號(hào)ID取而代之。

如需傳遞經(jīng)過(guò)混淆處理的帳號(hào)ID,請(qǐng)從擴(kuò)展程序API調(diào)用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);

向Google Play傳遞經(jīng)過(guò)混淆處理的個(gè)人資料ID

您可以向Google Play傳遞經(jīng)過(guò)混淆處理的個(gè)人資料ID以方便檢測(cè)欺詐行為,例如檢測(cè)是否有大量設(shè)備在短時(shí)間內(nèi)使用同一帳號(hào)進(jìn)行購(gòu)買(mǎi)。這與傳遞經(jīng)過(guò)混淆處理的用戶帳號(hào)ID類似。在這兩種情況下,ID都代表單個(gè)用戶,但是個(gè)人資料ID可以幫助您從單個(gè)應(yīng)用中的多份個(gè)人資料里唯一識(shí)別出單個(gè)用戶。向Google Play傳遞經(jīng)過(guò)混淆處理的個(gè)人資料ID后,您日后便可以在購(gòu)買(mǎi)收據(jù)中檢索此ID。

如需傳遞經(jīng)過(guò)混淆處理的個(gè)人資料ID,請(qǐng)?jiān)贗AP構(gòu)建器中調(diào)用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);

確認(rèn)訂閱的價(jià)格變動(dòng)

您可以通過(guò)Google Play更改有效訂閱的價(jià)格。游戲的用戶必須先確認(rèn)任何價(jià)格變動(dòng),然后更改才能生效。如需提示用戶確認(rèn)其訂閱的價(jià)格變動(dòng),請(qǐng)調(diào)用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結(jié)算服務(wù)插件時(shí),大多數(shù)API行為與其他應(yīng)用商店的Unity標(biāo)準(zhǔn)IAP實(shí)現(xiàn)的行為相同。但在某些情況下,API的行為會(huì)有所不同。本部分介紹了這些行為差異。

不支持開(kāi)發(fā)者載荷

Google Play已棄用開(kāi)發(fā)者載荷,并用更有意義且更相關(guān)的替代方法代替它。因此,API不支持開(kāi)發(fā)者載荷。如需詳細(xì)了解替代方法,請(qǐng)參閱開(kāi)發(fā)者載荷的相關(guān)頁(yè)面。

您可以繼續(xù)在其他應(yīng)用商店中使用Unity標(biāo)準(zhǔn)IAP實(shí)現(xiàn)所定義的接口,包括IStoreControllor。當(dāng)您提示購(gòu)買(mǎi)時(shí),您仍可使用IStoreControllor并調(diào)用InitiatePurchase()方法:

public void InitiatePurchase(Purchasing.Product product,string payload);

但是,您傳入的任何載荷都不會(huì)生效(不會(huì)出現(xiàn)在最終收據(jù)中)。

不支持SubscriptionManager

Unity IAP提供了管理訂閱的SubscriptionManager類。由于此類的Unity標(biāo)準(zhǔn)IAP實(shí)現(xiàn)使用開(kāi)發(fā)者載荷,因此不支持此類。您仍然可以創(chuàng)建此類,但是當(dāng)您使用該類的任何getter方法時(shí),您可能會(huì)收到不可靠的數(shù)據(jù)。

UpdateSubscription出現(xiàn)細(xì)微的API變更

Google Play結(jié)算服務(wù)插件不支持使用SubscriptionManager.UpdateSubscription()和SubscriptionManager.UpdateSubscriptionInGooglePlayStore()方法升級(jí)和降級(jí)您的訂閱。如果您的游戲調(diào)用了這些方法,系統(tǒng)會(huì)拋出GooglePlayStoreUnsupportedException。

結(jié)算庫(kù)提供了一個(gè)替代API來(lái)代替這些方法。如需升級(jí)或降級(jí)訂閱,請(qǐng)調(diào)用使用按比例計(jì)費(fèi)模式的UpdateSubscription()方法:

void UpdateSubscription(Product oldProduct,Product newProduct,

GooglePlayStoreProrationMode prorationMode=GooglePlayStoreProrationMode.Unknown);

您可以用平臺(tái)檢查封裝此方法調(diào)用,也可以在捕獲GooglePlayStoreUnsupportedeException時(shí)將其封裝在catch塊中。

如需了解按比例計(jì)費(fèi)模式的詳細(xì)使用方法和示例,請(qǐng)參閱設(shè)置按比例計(jì)費(fèi)模式。

立即登錄,閱讀全文
版權(quán)說(shuō)明:
本文內(nèi)容來(lái)自于Google Developers,本站不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。文章內(nèi)容系作者個(gè)人觀點(diǎn),不代表快出海對(duì)觀點(diǎn)贊同或支持。如有侵權(quán),請(qǐng)聯(lián)系管理員(zzx@kchuhai.com)刪除!
優(yōu)質(zhì)服務(wù)商推薦
更多
掃碼登錄
打開(kāi)掃一掃, 關(guān)注公眾號(hào)后即可登錄/注冊(cè)
加載中
二維碼已失效 請(qǐng)重試
刷新
賬號(hào)登錄/注冊(cè)
個(gè)人VIP
小程序
快出海小程序
公眾號(hào)
快出海公眾號(hào)
商務(wù)合作
商務(wù)合作
投稿采訪
投稿采訪
出海管家
出海管家