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());
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
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
UnityEngine.Object.FindObjectOfType
}
if (TheManager.currentScene == "MainGarage")
{
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
}
}
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
this.shopPurchase = false;
PlayerPrefs.SetInt("RemoveAds", 1);
}
else if (string.Equals(args.purchasedProduct.definition.id, IAPManager.car_2, StringComparison.Ordinal))
{
UnityEngine.Object.FindObjectOfType
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
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
}
if (TheManager.currentScene == "MainGarage")
{
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
}
}
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
UnityEngine.Object.FindObjectOfType
}
if (TheManager.currentScene == "MainGarage")
{
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
}
}
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
this.shopPurchase = false;
PlayerPrefs.SetInt("RemoveAds", 1);
}
else if (string.Equals(id, IAPManager.car_2, StringComparison.Ordinal))
{
UnityEngine.Object.FindObjectOfType
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
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
}
if (TheManager.currentScene == "MainGarage")
{
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
UnityEngine.Object.FindObjectOfType
}
}
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;
}



