Advertisement
Vagem

IAPStoreButton

Jul 30th, 2021 (edited)
1,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.Events;
  5. using UnityEngine.Purchasing;
  6. #if UNITY_EDITOR
  7. using System.Reflection;
  8. using UnityEditor.Events;
  9. #endif
  10.  
  11. namespace com.dod.spacezoo.iap
  12. {
  13.     using System;
  14.     using standards;
  15.  
  16.     [RequireComponent(typeof(Button))]
  17.     public class IAPStoreButton : MonoBehaviour
  18.     {
  19.         [SerializeField, IAPBundle] private string m_ProductID;
  20.         public string ProductID => m_ProductID;
  21.         public void ChangeProductID(string productID)
  22.         {
  23.             m_ProductID = productID;
  24.         }
  25.         [SerializeField] private bool m_ManualBuy;
  26.  
  27.         [Tooltip("Consume the product immediately after a successful purchase")]
  28.         [SerializeField] private bool m_ConsumePurchase;
  29.         public bool ConsumePurchase => m_ConsumePurchase;
  30.  
  31.         [Serializable] public class OnPurchaseCompletedEvent : UnityEvent<Product> { };
  32.         [Serializable] public class OnPurchaseFailedEvent : UnityEvent<Product, PurchaseFailureReason> { };
  33.  
  34.         [Header("Events")]
  35.         [Tooltip("Event fired after a successful purchase of this product")]
  36.         [SerializeField] private OnPurchaseCompletedEvent onPurchaseComplete;
  37.  
  38.         [Tooltip("Event fired after a failed purchase of this product")]
  39.         [SerializeField] private OnPurchaseFailedEvent onPurchaseFailed;
  40.  
  41.         [Header("UI")]
  42.         [Tooltip("[Optional] Displays the localized title from the app store")]
  43.         [SerializeField] private Text m_TitleText;
  44.         public Text TitleText => m_TitleText;
  45.  
  46.         [Tooltip("[Optional] Displays the localized description from the app store")]
  47.         [SerializeField] private Text m_DescriptionText;
  48.         public Text DescriptionText => m_DescriptionText;
  49.  
  50.         [Tooltip("[Optional] Displays the localized price from the app store")]
  51.         [SerializeField] private Text m_PriceText;
  52.         public Text PriceText => m_PriceText;
  53.  
  54.         public string LocalizedPriceString => IAPStoreHandler.IsInitialized ? IAPStoreHandler.Instance.GetProduct(m_ProductID).metadata.localizedPriceString : "--";
  55.  
  56.         public static List<IAPStoreButton> All { get; private set; } = new List<IAPStoreButton>();
  57.  
  58.         private void OnDisable()
  59.         {
  60.             All.Remove(this);
  61.         }
  62.  
  63.         private void OnEnable()
  64.         {
  65.             All.Add(this);
  66.  
  67.             if (IAPStoreHandler.IsInitialized)
  68.             {
  69.                 UpdateText();
  70.             }
  71.         }
  72.  
  73.         private void Start()
  74.         {
  75.             if(!m_ManualBuy)
  76.             {
  77.                 GetComponent<Button>().onClick.AddListener(PurchaseProduct);
  78.             }
  79.         }
  80.  
  81.         public void UpdateText()
  82.         {
  83.             if (!string.IsNullOrEmpty(m_ProductID))
  84.             {
  85.                 var product = IAPStoreHandler.Instance.GetProduct(m_ProductID);
  86.                 if (product != null)
  87.                 {
  88.                     if (m_TitleText != null)
  89.                     {
  90.                         m_TitleText.text = product.metadata.localizedTitle;
  91.                     }
  92.  
  93.                     if (m_DescriptionText != null)
  94.                     {
  95.                         m_DescriptionText.text = product.metadata.localizedDescription;
  96.                     }
  97.  
  98.                     if (m_PriceText != null)
  99.                     {
  100.                         m_PriceText.text = product.metadata.localizedPriceString;
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.  
  106.         public void PurchaseProduct()
  107.         {
  108.             IAPStoreHandler.Instance.StartPurchase(m_ProductID, this);
  109.         }
  110.  
  111.         public void OnPurchaseCompleted(Product product)
  112.         {
  113.             onPurchaseComplete.Invoke(product);
  114.         }
  115.  
  116.         public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
  117.         {
  118.             onPurchaseFailed.Invoke(product, reason);
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement