Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Events;
- using UnityEngine.Purchasing;
- #if UNITY_EDITOR
- using System.Reflection;
- using UnityEditor.Events;
- #endif
- namespace com.dod.spacezoo.iap
- {
- using System;
- using standards;
- [RequireComponent(typeof(Button))]
- public class IAPStoreButton : MonoBehaviour
- {
- [SerializeField, IAPBundle] private string m_ProductID;
- public string ProductID => m_ProductID;
- public void ChangeProductID(string productID)
- {
- m_ProductID = productID;
- }
- [SerializeField] private bool m_ManualBuy;
- [Tooltip("Consume the product immediately after a successful purchase")]
- [SerializeField] private bool m_ConsumePurchase;
- public bool ConsumePurchase => m_ConsumePurchase;
- [Serializable] public class OnPurchaseCompletedEvent : UnityEvent<Product> { };
- [Serializable] public class OnPurchaseFailedEvent : UnityEvent<Product, PurchaseFailureReason> { };
- [Header("Events")]
- [Tooltip("Event fired after a successful purchase of this product")]
- [SerializeField] private OnPurchaseCompletedEvent onPurchaseComplete;
- [Tooltip("Event fired after a failed purchase of this product")]
- [SerializeField] private OnPurchaseFailedEvent onPurchaseFailed;
- [Header("UI")]
- [Tooltip("[Optional] Displays the localized title from the app store")]
- [SerializeField] private Text m_TitleText;
- public Text TitleText => m_TitleText;
- [Tooltip("[Optional] Displays the localized description from the app store")]
- [SerializeField] private Text m_DescriptionText;
- public Text DescriptionText => m_DescriptionText;
- [Tooltip("[Optional] Displays the localized price from the app store")]
- [SerializeField] private Text m_PriceText;
- public Text PriceText => m_PriceText;
- public string LocalizedPriceString => IAPStoreHandler.IsInitialized ? IAPStoreHandler.Instance.GetProduct(m_ProductID).metadata.localizedPriceString : "--";
- public static List<IAPStoreButton> All { get; private set; } = new List<IAPStoreButton>();
- private void OnDisable()
- {
- All.Remove(this);
- }
- private void OnEnable()
- {
- All.Add(this);
- if (IAPStoreHandler.IsInitialized)
- {
- UpdateText();
- }
- }
- private void Start()
- {
- if(!m_ManualBuy)
- {
- GetComponent<Button>().onClick.AddListener(PurchaseProduct);
- }
- }
- public void UpdateText()
- {
- if (!string.IsNullOrEmpty(m_ProductID))
- {
- var product = IAPStoreHandler.Instance.GetProduct(m_ProductID);
- if (product != null)
- {
- if (m_TitleText != null)
- {
- m_TitleText.text = product.metadata.localizedTitle;
- }
- if (m_DescriptionText != null)
- {
- m_DescriptionText.text = product.metadata.localizedDescription;
- }
- if (m_PriceText != null)
- {
- m_PriceText.text = product.metadata.localizedPriceString;
- }
- }
- }
- }
- public void PurchaseProduct()
- {
- IAPStoreHandler.Instance.StartPurchase(m_ProductID, this);
- }
- public void OnPurchaseCompleted(Product product)
- {
- onPurchaseComplete.Invoke(product);
- }
- public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
- {
- onPurchaseFailed.Invoke(product, reason);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement