IAPDemoProductUI.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #if UNITY_PURCHASING
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Purchasing;
  6. public class IAPDemoProductUI : MonoBehaviour
  7. {
  8. public Button purchaseButton;
  9. public Button receiptButton;
  10. public Text titleText;
  11. public Text descriptionText;
  12. public Text priceText;
  13. public Text statusText;
  14. private string m_ProductID;
  15. private Action<string> m_PurchaseCallback;
  16. private string m_Receipt;
  17. public void SetProduct(Product p, Action<string> purchaseCallback)
  18. {
  19. titleText.text = p.metadata.localizedTitle;
  20. descriptionText.text = p.metadata.localizedDescription;
  21. priceText.text = p.metadata.localizedPriceString;
  22. receiptButton.interactable = p.hasReceipt;
  23. m_Receipt = p.receipt;
  24. m_ProductID = p.definition.id;
  25. m_PurchaseCallback = purchaseCallback;
  26. statusText.text = p.availableToPurchase ? "Available" : "Unavailable";
  27. }
  28. public void SetPendingTime(int secondsRemaining)
  29. {
  30. statusText.text = "Pending " + secondsRemaining.ToString();
  31. }
  32. public void PurchaseButtonClick()
  33. {
  34. if (m_PurchaseCallback != null && !string.IsNullOrEmpty(m_ProductID))
  35. {
  36. m_PurchaseCallback(m_ProductID);
  37. }
  38. }
  39. public void ReceiptButtonClick()
  40. {
  41. if (!string.IsNullOrEmpty(m_Receipt))
  42. Debug.Log("Receipt for " + m_ProductID + ": " + m_Receipt);
  43. }
  44. }
  45. #endif