Advertisement
_EagleOwle_

DonateShopItem

Dec 13th, 2021
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Purchasing;
  3. using TMPro;
  4. using System.Linq;
  5.  
  6. public class DonateShopItem : ShopItem
  7. {
  8.     [SerializeField] public IAPButton _purchaseBtn;
  9.     [SerializeField] private TMP_Text _countText;
  10.  
  11.     public override void Init(ShopItemConfig config, bool isPurchased = false)
  12.     {
  13.         base.Init(config, isPurchased);
  14.  
  15.         if (StoreListener.Instance.StoreController == null)
  16.         {
  17.             CustomDebug.Debug.Log("Warning: StoreListener.Instance.StoreController is null");
  18.             Debug.Log("Warning: StoreListener.Instance.StoreController is null");
  19.             return;
  20.         }
  21.         else
  22.         {
  23.            
  24.             _purchaseBtn.productId = (config as DiamondItemConfig).StoreItemID;
  25.             Product product = StoreListener.Instance.StoreController.products.WithID(_purchaseBtn.productId);
  26.             _costText.text = product.metadata.localizedPrice.ToString() + " " + product.metadata.isoCurrencyCode;
  27.             _countText.text = product.definition.payout.quantity.ToString();
  28.             //_purchaseBtn.onPurchaseComplete.AddListener(PurchaseComplete);
  29.            // _purchaseBtn.onPurchaseFailed.AddListener(PurchaseFailed);
  30.  
  31.             CustomDebug.Debug.Log("Init product: " + product.definition.id);
  32.             _purchaseBtn.enabled = true;
  33.         }
  34.        
  35.        
  36.         /*
  37.         _purchaseBtn.productId = (config as DiamondItemConfig).StoreItemID;
  38.         _purchaseBtn.enabled = true;
  39.        
  40.         _countText.text = ((MPIConfig) config).Count.ToString();
  41.  
  42.         if (StoreListener.Instance.StoreController == null)
  43.         {
  44.             CustomDebug.Debug.Log("Warning: StoreListener.Instance.StoreController is null");
  45.             Debug.Log("Warning: StoreListener.Instance.StoreController is null");
  46.             _costText.text = ((DiamondItemConfig) config).Price + " USD";
  47.             return;
  48.         }
  49.  
  50.         Product product = StoreListener.Instance.StoreController.products.WithID(_purchaseBtn.productId);
  51.         _costText.text = product.metadata.localizedPrice + " " + product.metadata.isoCurrencyCode;
  52.         // _countText.text = product.definition.payout.quantity.ToString();
  53.         _purchaseBtn.onPurchaseComplete.AddListener(Purchase);
  54.         CustomDebug.Debug.Log("ADD PurchaseProduct TO BUTTON");
  55.        */
  56.     }
  57.  
  58.     public void PurchaseComplete(Product product)
  59.     {
  60.         CustomDebug.Debug.Log("PurchaseComplete: " + product.definition.id);
  61.         SLS.Data.Game.Diamonds.Value += (int) product.definition.payout.quantity;
  62.         SLS.Data.Settings.AdsEnabled.Value = false;
  63.         AudioController.PlayClipAtPosition(_buttonClip, transform.position);
  64.     }
  65.  
  66.     public void PurchaseFailed(Product product, PurchaseFailureReason reason)
  67.     {
  68.         CustomDebug.Debug.Log("PurchaseFailed: " + reason.ToString());
  69.     }
  70.  
  71.     public override void Purchase() { }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement