/** * Increment the product class amount can be used to increment the value of a product class * for instance if you have 3 products which gives you gold coins you might also want the player * to be able to pickup gold coins in the game. You can use this to increment the value that the * player pick's up in the game. */ public static void IncrementProductClassAmount(string productClass, int incrementWith) { var prod = ShopConfig.GetProductByClassId(productClass); if (prod != null) { prod.AddValueToProductAmountAndSaveLocalData(incrementWith); } else { Debug.LogError("MobyShop: Couldn't find a Product With Class Name : " + productClass + "\nAvailable classes :" + string.Join(",", ShopConfig.ProductClasses)); // Error porduct doesnt exists... } }
/** * This method initializes the inapp purchase. */ static void BillingUnityEditor_BuyProductInGameCurrency(ProductInfo product, System.Action <bool, string, Shop.BuyResponse> callback, ShopConfirm shopConfirmInterface, ShopNotEnoughCoins notEnoughCoinsInterface) { if (MobyShop.Shop.Verbose) { Debug.Log("MobyShop: Buying product with virtual currency (" + product.ingameCurrencyClass + "); price=" + product.PriceTag + "; currency class=" + product.ingameCurrencyClass); } if (product.billing != BillingType.IngameCurrency) { throw new System.Exception("MobyShop: Billing with virtual currency is only allowed for : " + product.ProductId); } // Currency Class if (string.IsNullOrEmpty(product.ingameCurrencyClass)) { Debug.LogError("MobyShop: Error currecy class did not exist : '" + product.ingameCurrencyClass + "'"); callback(false, "Invalid product setup - no currency class given", Shop.BuyResponse.Failed); } if (ShopConfig.GetProductByClassId(product.ingameCurrencyClass) == null) { Debug.Log("MobyShop: Warning: No Product's exists with currency class id : '" + product.ingameCurrencyClass + "'\nNOTE: This might be okay if you don't intent for the user to buy the currency in the game shop"); } // Debug.Log( "MobyShop: Buy product with virtual currency (coins)" ); int amount = Mathf.Abs(Shop.GetProductClassAmount(product.ingameCurrencyClass)); if (amount < Mathf.Abs(product.price)) { //Debug.Log ("Not enough coins"); if (notEnoughCoinsInterface == null) { var uigo = instance.NotEnoughCoins; if (uigo == null) { if (Billing.instance.PrefabNotEnoughCoins == null) { Debug.LogError("MobyShop: Error 'Not Enough Coins' Dialog Prefab was not found"); callback(false, "", Shop.BuyResponse.Cancelled); return; } var goNotEn = GameObject.Instantiate(Billing.instance.PrefabNotEnoughCoins); uigo = goNotEn; } var canvas = CanvasHelper.GetCanvas(); uigo.transform.localPosition = Vector2.zero; uigo.gameObject.SetActive(true); uigo.transform.parent = canvas.transform; var rt = uigo.GetComponent <RectTransform> (); rt.FitAnchorsToCorners(); uigo.GetComponentCompatibleWith <global::MobyShop.ShopNotEnoughCoins> ().onDismissed = (global::MobyShop.ShopNotEnoughCoins.Dismissed dismissedWith) => { var response = dismissedWith == global::MobyShop.ShopNotEnoughCoins.Dismissed.BuyMoreCoins ? Shop.BuyResponse.BuyMoreCoins : Shop.BuyResponse.Cancelled; callback(false, "Not enough coins available", response); }; return; } else { notEnoughCoinsInterface.onDismissed = (ShopNotEnoughCoins.Dismissed dismissedWith) => { var response = dismissedWith == ShopNotEnoughCoins.Dismissed.BuyMoreCoins ? Shop.BuyResponse.BuyMoreCoins : Shop.BuyResponse.Cancelled; callback(false, "Not enough coins available", response); }; notEnoughCoinsInterface.Show(); return; } //return; } else { // if (shopConfirmInterface == null) { var uigo = MobyShop.Shop.BillingIngameCurrentUI; if (uigo == null) { callback(false, "Failed to get UI confirm object.", Shop.BuyResponse.Failed); return; } var canvas = CanvasHelper.GetCanvas(); uigo.transform.localPosition = Vector2.zero; uigo.gameObject.SetActive(true); uigo.transform.parent = canvas.transform; var rt = uigo.GetComponent <RectTransform> (); rt.offsetMin = rt.offsetMax = Vector2.zero; rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; uigo.GetComponentCompatibleWith <global::ShopConfirm> ().onDismissed = (BillingInGameCurrency.AcceptOrCancel result) => { //Debug.Log("OnBuyResult : " + result ); if (result == BillingInGameCurrency.AcceptOrCancel.Accepted) { Shop.IncrementProductClassAmount(product.ingameCurrencyClass, -Mathf.Abs(product.price)); CallUnlockProduct(BoughtOrRestored.Bought, product); callback(true, "", Shop.BuyResponse.Ok); } else { callback(false, "User cancelled", Shop.BuyResponse.Cancelled); } }; } else { shopConfirmInterface.onDismissed = (BillingInGameCurrency.AcceptOrCancel result) => { if (result == BillingInGameCurrency.AcceptOrCancel.Accepted) { Shop.IncrementProductClassAmount(product.ingameCurrencyClass, -Mathf.Abs(product.price)); CallUnlockProduct(BoughtOrRestored.Bought, product); callback(true, "", Shop.BuyResponse.Ok); } else { callback(false, "User cancelled", Shop.BuyResponse.Cancelled); } }; shopConfirmInterface.Show( ); return; } } }