unity--google支付

Posted 格拉格拉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity--google支付相关的知识,希望对你有一定的参考价值。

1.配置商品

 

 2.配置google 支付key

 

 

 

3.支付管理脚本

using System;
using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.Purchasing.Extension;
using UnityEngine.Purchasing.Security;

public class IAPManager : MonoBehaviour, IStoreListener

    private void Start()
   
        TheManager.iapManager = this;
        if (IAPManager.m_StoreController == null)
       
            this.InitializePurchasing();
       
   

    public void InitializePurchasing()
   
        if (this.IsInitialized())
       
            return;
       
        ConfigurationBuilder configurationBuilder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()/*, new IPurchasingModule[0]*/);
        configurationBuilder.AddProduct(IAPManager.diamondPack_1, ProductType.Consumable);
        configurationBuilder.AddProduct(IAPManager.diamondPack_2, ProductType.Consumable);
        configurationBuilder.AddProduct(IAPManager.diamondPack_3, ProductType.Consumable);
        configurationBuilder.AddProduct(IAPManager.diamondPack_4, ProductType.Consumable);
        configurationBuilder.AddProduct(IAPManager.cash_1, ProductType.Consumable);
        configurationBuilder.AddProduct(IAPManager.cash_2, ProductType.Consumable);
        configurationBuilder.AddProduct(IAPManager.cash_3, ProductType.Consumable);
        configurationBuilder.AddProduct(IAPManager.cash_4, ProductType.Consumable);
        //configurationBuilder.AddProduct(IAPManager.removeAds, ProductType.NonConsumable);
        //configurationBuilder.AddProduct(IAPManager.car_1, ProductType.NonConsumable);
        //configurationBuilder.AddProduct(IAPManager.car_2, ProductType.NonConsumable);
        configurationBuilder.AddProduct(IAPManager.premium, ProductType.NonConsumable);
        UnityPurchasing.Initialize(this, configurationBuilder);
   

    private bool IsInitialized()
   
        return IAPManager.m_StoreController != null && IAPManager.m_StoreExtensionProvider != null;
   

    public void BuyDiamondPack1()
   
        this.BuyProductID(IAPManager.diamondPack_1);
   

    public void BuyDiamondPack2()
   
        this.BuyProductID(IAPManager.diamondPack_2);
   

    public void BuyDiamondPack3()
   
        this.BuyProductID(IAPManager.diamondPack_3);
   

    public void BuyDiamondPack4()
   
        this.BuyProductID(IAPManager.diamondPack_4);
   

    public void BuyCash1()
   
        this.BuyProductID(IAPManager.cash_1);
   

    public void BuyCash2()
   
        this.BuyProductID(IAPManager.cash_2);
   

    public void BuyCash3()
   
        this.BuyProductID(IAPManager.cash_3);
   

    public void BuyCash4()
   
        this.BuyProductID(IAPManager.cash_4);
   

    public void BuyRemoveAds()
   
        this.BuyProductID(IAPManager.removeAds);
   

    public void BuyCar1()
   
        this.BuyProductID(IAPManager.car_1);
   

    public void BuyCar2()
   
        this.BuyProductID(IAPManager.car_2);
   

    public void BuyPremium()
   
        AdsControl.instance.SendEvent_firebase("enter_premium", "","");
        this.BuyProductID(IAPManager.premium);
   

    private void BuyProductID(string productId)
   
        Debug.LogError("购买商品:"+productId);
        if (AdsControl.instance.isTest)
            ProcessPurchase(productId);
            return;
       

        if (this.IsInitialized())
       
            Product product = IAPManager.m_StoreController.products.WithID(productId);
            if (product != null && product.availableToPurchase)
           
                UnityEngine.Debug.Log(string.Format("Purchasing product asychronously: '0'", product.definition.id));
                IAPManager.m_StoreController.InitiatePurchase(product);
           
            else
           
                UnityEngine.Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
           
       
        else
       
            UnityEngine.Debug.Log("BuyProductID FAIL. Not initialized.");
       
   

    public void RestorePurchases()
   
        if (!this.IsInitialized())
       
            UnityEngine.Debug.Log("RestorePurchases FAIL. Not initialized.");
            return;
       
        if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.OSXPlayer)
       
            UnityEngine.Debug.Log("RestorePurchases started ...");
            IAppleExtensions extension = IAPManager.m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
            extension.RestoreTransactions(delegate (bool result)
           
                UnityEngine.Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
            );
       
        else
       
            UnityEngine.Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
       
   

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
   
        UnityEngine.Debug.Log("OnInitialized: Success");
        IAPManager.m_StoreController = controller;
        IAPManager.m_StoreExtensionProvider = extensions;

        // 这里可以获取您在AppStore和Google Play 上配置的商品;
        //ProductCollection products = m_StoreController.products;
        //Product[] all = products.all;
        //for (int i = 0; i < all.Length; i++)
        //
        //    Debug.LogError(all[i].metadata.localizedTitle + "|" + all[i].metadata.localizedPriceString + "|" + all[i].metadata.localizedDescription + "|" + all[i].metadata.isoCurrencyCode);
        //

        #if UNITY_ios
        // m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
        #endif
   

    public void OnInitializeFailed(InitializationFailureReason error)
   
        UnityEngine.Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
   

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
   
        Debug.LogError("pay success, begain reward...");
        //bool flag = true;
        //CrossPlatformValidator crossPlatformValidator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.identifier);
        //try
        //
        //    IPurchaseReceipt[] array = crossPlatformValidator.Validate(args.purchasedProduct.receipt);
        //
        //catch (IAPSecurityException)
        //
        //    flag = false;
        //
        //if (flag)
        //
            Debug.LogError("aaaaa");
            if (TheManager.shopManager)
           
                TheManager.shopManager.purchasedDiamondParent.SetActive(false);
                TheManager.shopManager.purchasedCashParent.SetActive(false);
           
            int @int = PlayerPrefs.GetInt("Diamond");
            int int2 = PlayerPrefs.GetInt("Cash");
            if (string.Equals(args.purchasedProduct.definition.id, IAPManager.diamondPack_1, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Diamond", @int + 100);
                this.shopPurchase = true;
                PlayerPrefs.SetInt("RemoveAds", 1);
                TheManager.shopManager.packPower = 1;
                TheManager.shopManager.purchasedDiamondParent.SetActive(true);
                TheManager.shopManager.purchasedDiamondLabel.text = 100.ToString("n0");
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.diamondPack_2, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Diamond", @int + 220);
                this.shopPurchase = true;
                PlayerPrefs.SetInt("RemoveAds", 1);
                TheManager.shopManager.packPower = 2;
                TheManager.shopManager.purchasedDiamondParent.SetActive(true);
                TheManager.shopManager.purchasedDiamondLabel.text = 220.ToString("n0");
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.diamondPack_3, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Diamond", @int + 600);
                this.shopPurchase = true;
                PlayerPrefs.SetInt("RemoveAds", 1);
                TheManager.shopManager.packPower = 3;
                TheManager.shopManager.purchasedDiamondParent.SetActive(true);
                TheManager.shopManager.purchasedDiamondLabel.text = 600.ToString("n0");
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.diamondPack_4, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Diamond", @int + 1400);
                this.shopPurchase = true;
                PlayerPrefs.SetInt("RemoveAds", 1);
                TheManager.shopManager.packPower = 4;
                TheManager.shopManager.purchasedDiamondParent.SetActive(true);
                TheManager.shopManager.purchasedDiamondLabel.text = 140.ToString("n0");
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.removeAds, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("RemoveAds", 1);
                this.shopPurchase = false;
                if (TheManager.currentScene == "Freeroam")
               
                    UnityEngine.Object.FindObjectOfType<MenuController>().removeAdsPauseButton.SetActive(false);
                    UnityEngine.Object.FindObjectOfType<MenuController>().removedAdsObjectPause.SetActive(true);
               
                if (TheManager.currentScene == "MainGarage")
               
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().removeAdsButton.SetActive(false);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().removedAdsObject.SetActive(true);
               
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.cash_1, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Cash", int2 + 250000);
                this.shopPurchase = true;
                PlayerPrefs.SetInt("RemoveAds", 1);
                TheManager.shopManager.packPower = 1;
                TheManager.shopManager.purchasedCashParent.SetActive(true);
                TheManager.shopManager.purchasedCashLabel.text = 250000.ToString("n0");
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.cash_2, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Cash", int2 + 600000);
                this.shopPurchase = true;
                PlayerPrefs.SetInt("RemoveAds", 1);
                TheManager.shopManager.packPower = 2;
                TheManager.shopManager.purchasedCashParent.SetActive(true);
                TheManager.shopManager.purchasedCashLabel.text = 600000.ToString("n0");
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.cash_3, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Cash", int2 + 1500000);
                this.shopPurchase = true;
                PlayerPrefs.SetInt("RemoveAds", 1);
                TheManager.shopManager.packPower = 3;
                TheManager.shopManager.purchasedCashParent.SetActive(true);
                TheManager.shopManager.purchasedCashLabel.text = 1500000.ToString("n0");
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.cash_4, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Cash", int2 + 4000000);
                this.shopPurchase = true;
                PlayerPrefs.SetInt("RemoveAds", 1);
                TheManager.shopManager.packPower = 4;
                TheManager.shopManager.purchasedCashParent.SetActive(true);
                TheManager.shopManager.purchasedCashLabel.text = 4000000.ToString("n0");
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.car_1, StringComparison.Ordinal))
           
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().CarBuy();
                this.shopPurchase = false;
                PlayerPrefs.SetInt("RemoveAds", 1);
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.car_2, StringComparison.Ordinal))
           
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().CarBuy();
                this.shopPurchase = false;
                PlayerPrefs.SetInt("RemoveAds", 1);
           
            else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.premium, StringComparison.Ordinal))
           
                PlayerPrefs.SetInt("Premium", 1);
                PlayerPrefs.SetInt("RemoveAds", 1);
                this.shopPurchase = false;
                if (TheManager.currentScene == "Gameplay")
               
                    UnityEngine.Object.FindObjectOfType<MenuController>().premiumButtonPause.SetActive(false);
                    UnityEngine.Object.FindObjectOfType<MenuController>().premiumEnabledPause.SetActive(true);
                    UnityEngine.Object.FindObjectOfType<MenuController>().premiumPurchasedParent.SetActive(true);
               
                if (TheManager.currentScene == "MainGarage")
               
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumButtonMain.SetActive(false);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumEnabledMain.SetActive(true);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumButtonSettings.SetActive(false);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().askForPremiumParent.SetActive(false);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumEnabledSettings.SetActive(true);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumCustomizationParent.SetActive(false);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().normalCustomizationParent.SetActive(true);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().nitroLockedParent.SetActive(false);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().nitroUnlockedParent.SetActive(true);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumPurchasedParent.SetActive(true);
                    UnityEngine.Object.FindObjectOfType<MainGarageManager>().DisableAllPremiumObjects();
               
           
            if (this.shopPurchase)
           
                TheManager.shopManager.SetWalletValues();
                TheManager.shopManager.TransactionCompleted();
           
        //
        //else if (this.shopPurchase)
        //
        //    Debug.LogError("bbbbbb");
        //    TheManager.shopManager.TransactionDenied();
        //
        return PurchaseProcessingResult.Complete;
   
    public void ProcessPurchase(string id)
   
        Debug.LogError("发奖:"+id);
        if (TheManager.shopManager)
       
            TheManager.shopManager.purchasedDiamondParent.SetActive(false);
            TheManager.shopManager.purchasedCashParent.SetActive(false);
       
        int @int = PlayerPrefs.GetInt("Diamond");
        int int2 = PlayerPrefs.GetInt("Cash");
        if (string.Equals(id, IAPManager.diamondPack_1, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Diamond", @int + 100);
            this.shopPurchase = true;
            PlayerPrefs.SetInt("RemoveAds", 1);
            TheManager.shopManager.packPower = 1;
            TheManager.shopManager.purchasedDiamondParent.SetActive(true);
            TheManager.shopManager.purchasedDiamondLabel.text = 100.ToString("n0");
       
        else if (string.Equals(id, IAPManager.diamondPack_2, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Diamond", @int + 220);
            this.shopPurchase = true;
            PlayerPrefs.SetInt("RemoveAds", 1);
            TheManager.shopManager.packPower = 2;
            TheManager.shopManager.purchasedDiamondParent.SetActive(true);
            TheManager.shopManager.purchasedDiamondLabel.text = 220.ToString("n0");
       
        else if (string.Equals(id, IAPManager.diamondPack_3, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Diamond", @int + 600);
            this.shopPurchase = true;
            PlayerPrefs.SetInt("RemoveAds", 1);
            TheManager.shopManager.packPower = 3;
            TheManager.shopManager.purchasedDiamondParent.SetActive(true);
            TheManager.shopManager.purchasedDiamondLabel.text = 600.ToString("n0");
       
        else if (string.Equals(id, IAPManager.diamondPack_4, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Diamond", @int + 1400);
            this.shopPurchase = true;
            PlayerPrefs.SetInt("RemoveAds", 1);
            TheManager.shopManager.packPower = 4;
            TheManager.shopManager.purchasedDiamondParent.SetActive(true);
            TheManager.shopManager.purchasedDiamondLabel.text = 1400.ToString("n0");
       
        else if (string.Equals(id, IAPManager.removeAds, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("RemoveAds", 1);
            this.shopPurchase = false;
            if (TheManager.currentScene == "Freeroam")
           
                UnityEngine.Object.FindObjectOfType<MenuController>().removeAdsPauseButton.SetActive(false);
                UnityEngine.Object.FindObjectOfType<MenuController>().removedAdsObjectPause.SetActive(true);
           
            if (TheManager.currentScene == "MainGarage")
           
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().removeAdsButton.SetActive(false);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().removedAdsObject.SetActive(true);
           
       
        else if (string.Equals(id, IAPManager.cash_1, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Cash", int2 + 250000);
            this.shopPurchase = true;
            PlayerPrefs.SetInt("RemoveAds", 1);
            TheManager.shopManager.packPower = 1;
            TheManager.shopManager.purchasedCashParent.SetActive(true);
            TheManager.shopManager.purchasedCashLabel.text = 250000.ToString("n0");
       
        else if (string.Equals(id, IAPManager.cash_2, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Cash", int2 + 600000);
            this.shopPurchase = true;
            PlayerPrefs.SetInt("RemoveAds", 1);
            TheManager.shopManager.packPower = 2;
            TheManager.shopManager.purchasedCashParent.SetActive(true);
            TheManager.shopManager.purchasedCashLabel.text = 600000.ToString("n0");
       
        else if (string.Equals(id, IAPManager.cash_3, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Cash", int2 + 1500000);
            this.shopPurchase = true;
            PlayerPrefs.SetInt("RemoveAds", 1);
            TheManager.shopManager.packPower = 3;
            TheManager.shopManager.purchasedCashParent.SetActive(true);
            TheManager.shopManager.purchasedCashLabel.text = 1500000.ToString("n0");
       
        else if (string.Equals(id, IAPManager.cash_4, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Cash", int2 + 4000000);
            this.shopPurchase = true;
            PlayerPrefs.SetInt("RemoveAds", 1);
            TheManager.shopManager.packPower = 4;
            TheManager.shopManager.purchasedCashParent.SetActive(true);
            TheManager.shopManager.purchasedCashLabel.text = 4000000.ToString("n0");
       
        else if (string.Equals(id, IAPManager.car_1, StringComparison.Ordinal))
       
            UnityEngine.Object.FindObjectOfType<MainGarageManager>().CarBuy();
            this.shopPurchase = false;
            PlayerPrefs.SetInt("RemoveAds", 1);
       
        else if (string.Equals(id, IAPManager.car_2, StringComparison.Ordinal))
       
            UnityEngine.Object.FindObjectOfType<MainGarageManager>().CarBuy();
            this.shopPurchase = false;
            PlayerPrefs.SetInt("RemoveAds", 1);
       
        else if (string.Equals(id, IAPManager.premium, StringComparison.Ordinal))
       
            PlayerPrefs.SetInt("Premium", 1);
            PlayerPrefs.SetInt("RemoveAds", 1);
            this.shopPurchase = false;
            if (TheManager.currentScene == "Gameplay")
           
                UnityEngine.Object.FindObjectOfType<MenuController>().premiumButtonPause.SetActive(false);
                UnityEngine.Object.FindObjectOfType<MenuController>().premiumEnabledPause.SetActive(true);
                UnityEngine.Object.FindObjectOfType<MenuController>().premiumPurchasedParent.SetActive(true);
           
            if (TheManager.currentScene == "MainGarage")
           
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumButtonMain.SetActive(false);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumEnabledMain.SetActive(true);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumButtonSettings.SetActive(false);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().askForPremiumParent.SetActive(false);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumEnabledSettings.SetActive(true);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumCustomizationParent.SetActive(false);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().normalCustomizationParent.SetActive(true);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().nitroLockedParent.SetActive(false);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().nitroUnlockedParent.SetActive(true);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().premiumPurchasedParent.SetActive(true);
                UnityEngine.Object.FindObjectOfType<MainGarageManager>().DisableAllPremiumObjects();
           
       
        if (this.shopPurchase)
       
            TheManager.shopManager.SetWalletValues();
            TheManager.shopManager.TransactionCompleted();
       
   
    public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
   
        Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '0', PurchaseFailureReason: 1", product.definition.storeSpecificId, failureReason));
   

    private static IStoreController m_StoreController;

    private static IExtensionProvider m_StoreExtensionProvider;

    public static string diamondPack_1 = "diamonds_1.99"; //ultimate_car_diamond_1

    public static string diamondPack_2 = "diamonds_3.99"; //ultimate_car_diamond_2

    public static string diamondPack_3 = "diamonds_9.99"; //ultimate_car_diamond_3

    public static string diamondPack_4 = "diamonds_19.99"; //ultimate_car_diamond_4

    public static string cash_1 = "cash_1.99"; //ultimate_car_cash_1

    public static string cash_2 = "cash_3.99"; //ultimate_car_cash_2

    public static string cash_3 = "cash_9.99"; //ultimate_car_cash_3

    public static string cash_4 = "cash_19.99"; //ultimate_car_cash_4

    public static string removeAds = "ultimate_car_remove_ads";

    public static string car_1 = "ultimate_car_car1";

    public static string car_2 = "ultimate_car_car2";

    public static string premium = "premium_4.99";  //ultimate_car_premium

    private bool shopPurchase;

 

以上是关于unity--google支付的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D - Google Play 64 位要求的问题

Unity Google Play 游戏 IsConnectedToRoom 检查

如何将点云库与 Unity Google tango 项目集成?

Unity AdMob 插件 2.3.1 版和 Unity Google Play 游戏插件 0.9.30 不能一起使用

Unity Google Play 游戏插件无法在 Xcode iOS 中编译

实现成就和排行榜的问题 Unity、Android - Google-Play-Games