搭配 Unity 使用 Google Play 帳款服務程式庫

Google Play 帳款服務外掛程式為應用程式內購擴充 Unity 的內建服務與資產 (稱為 Unity IAP),為您的遊戲提供 Google Play 帳款服務程式庫的所有最新功能。本指南說明如何設定專案以使用該外掛程式,以及如何使用 Unity 在遊戲中實作 Google Play 帳款服務程式庫功能。

設定 Google Play 帳款服務外掛程式

若要設定外掛程式,請完成以下各個連結章節中的步驟:

  1. 啟用 Unity IAP 抽象層
  2. 下載並匯入外掛程式
  3. 設定外掛程式的版本設定
  4. 啟用外掛程式

啟用 Unity IAP 抽象層

Google Play 帳款服務外掛程式以 Unity IAP 內建的抽象層為基礎,因此您必須先啟用這個抽象層,然後再下載並匯入外掛程式。如要啟用 Unity IAP 抽象層,請按照下列指示操作:

  1. 完成以下 Unity 教學課程中的所有步驟:為專案設定 Unity 服務
  2. 完成以下 Unity 教學課程中的所有步驟:啟用 Unity IAP 服務

下載並匯入外掛程式

外掛程式是以 .unitypackage 格式的 Unity 套件形式提供。請按照下列步驟下載並匯入外掛程式:

  1. 在存放區的 GitHub 版本頁面中,下載最新版的 Unity 專用 Google Play 外掛程式。
  2. 在 Unity 選單列中,依序點選「Assets」>「Import Package」>「Custom Package」

  3. 找出 .unitypackage 檔案的下載位置,然後選取該檔案。

  4. 在「Import Unity Package」對話方塊中,選取所有資產並按一下「Import」

匯入套件後,系統會將名為「GooglePlayPlugins」的新資料夾 (位於「Assets」資料夾的根層級) 新增至專案的資產中。這個資料夾包含適用於該外掛程式的所有 Google Play 帳款服務程式庫資產。

調整建構設定

由於外掛程式會擴充 Unity IAP 功能,因此除非從該版本移除 Unity IAP 中部分較舊的重疊依附元件,否則 Unity 會產生衝突,並且無法建構 Android APK。這個外掛程式會自動移除專案中發生衝突的程式庫。請按照下列步驟解決這些衝突:

  1. 在 Unity 選單列中,依序選取「Google」>「Play Billing」>「Build Settings」

  2. 在「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 支援延後購買交易 (也稱為「未完成交易」或「未完成購買交易」),讓使用者可以先行購買,再到商店使用現金完成交易。

若要啟用延後購買交易功能,請呼叫 EnableDeferredPurchase() 方法,以 IAP 建構工具修改模組設定:

// 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 以便偵測濫用行為,例如偵測是否有許多裝置在短時間內透過同一個帳戶進行購買交易。

請從擴充的 API 中呼叫 SetObfuscatedAccountId() 方法,傳遞經模糊化處理的帳戶 ID:

// 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,請呼叫 SetObfuscatedProfileId() 方法,以 IAP 建構工具修改模組設定:

// 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 區塊。

如要進一步瞭解按比例計費模式的使用方法和範例,請參閱設定按比例計費模式