为什么在导入Google Play游戏服务后Unity IAP无法工作

时间:2018-12-19 14:06:19

标签: unity3d google-play in-app-purchase

Unity IAP在导入Google Play游戏服务后无法运行,在安装Google Play游戏服务前可以运行。成功初始化成功,失败后没有触发“ OnPurchaseFailed”,为什么?

这是代码:

using System;
using UnityEngine;
using UnityEngine.Purchasing;

public class Purchaser : MonoBehaviour, IStoreListener
{
    private static IStoreController m_StoreController;          // The Unity Purchasing system.
    private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.

    public static string NO_ADS = "no_ads";
    public static string MORE_COIN = "more_coin";

    void Start()
    {
        if (m_StoreController == null)
            InitializePurchasing();

    }

    public void InitializePurchasing()
    {
        if (IsInitialized())
            return;

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

        builder.AddProduct(NO_ADS, ProductType.NonConsumable);
        builder.AddProduct(MORE_COIN, ProductType.NonConsumable);

        UnityPurchasing.Initialize(this, builder);
    }

    bool IsInitialized()
    {
        return m_StoreController != null && m_StoreExtensionProvider != null;
    }

    public void RemoveAds()
    {
        BuyProductID(NO_ADS);
    }

    public void MoreCoin()
    {
        BuyProductID(MORE_COIN);
    }

    void BuyProductID(string productId)
    {
        if (IsInitialized())
        {
            Product product = m_StoreController.products.WithID(productId);

            if (product != null && product.availableToPurchase)
                m_StoreController.InitiatePurchase(product);
        }
    }

    //IStoreListener

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        m_StoreController = controller;
        m_StoreExtensionProvider = extensions;

        //m_StoreController.products.WithID(NO_ADS).metadata.localizedPriceString;
        //m_StoreController.products.WithID(NO_ADS).metadata.isoCurrencyCode;
    }

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    {
        // A consumable product has been purchased by this user.
        if (String.Equals(args.purchasedProduct.definition.id, NO_ADS, StringComparison.Ordinal))
            AdManager.instance.RemoveADS();
        else if (String.Equals(args.purchasedProduct.definition.id, MORE_COIN, StringComparison.Ordinal))
        {
            PlayerPrefs.SetInt("MoreCoin", 1);
            Menu.instance.UpdateStore();
        }

        return PurchaseProcessingResult.Complete;
    }

    public void OnInitializeFailed(InitializationFailureReason error)
    {
        // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
        Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    }


    public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    {
        // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
        // this reason with the user to guide their troubleshooting actions.
        Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    }
}

1 个答案:

答案 0 :(得分:0)

90% consistent with your code页面上:

// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
// asynchronously.
m_StoreController.InitiatePurchase(product);

您需要在Debug.Log("got here")ProcessPurchase方法中利用OnPurchaseFailed来确定购买是否有效。