1
0

IAPButtonEditor.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using UnityEngine;
  2. using UnityEngine.Purchasing;
  3. using System.Collections.Generic;
  4. using static UnityEditor.Purchasing.UnityPurchasingEditor;
  5. namespace UnityEditor.Purchasing
  6. {
  7. /// <summary>
  8. /// Customer Editor class for the IAPButton. This class handle how the IAPButton should represent itself in the UnityEditor.
  9. /// </summary>
  10. [CustomEditor(typeof(IAPButton))]
  11. [CanEditMultipleObjects]
  12. public class IAPButtonEditor : Editor
  13. {
  14. private static readonly string[] excludedFields = new string[] { "m_Script" };
  15. private static readonly string[] restoreButtonExcludedFields = new string[] { "m_Script", "consumePurchase", "onPurchaseComplete", "onPurchaseFailed", "titleText", "descriptionText", "priceText" };
  16. private const string kNoProduct = "<None>";
  17. private List<string> m_ValidIDs = new List<string>();
  18. private SerializedProperty m_ProductIDProperty;
  19. /// <summary>
  20. /// Event trigger when IAPButton is enabled in the scene.
  21. /// </summary>
  22. public void OnEnable()
  23. {
  24. m_ProductIDProperty = serializedObject.FindProperty("productId");
  25. }
  26. /// <summary>
  27. /// Event trigger when trying to draw the IAPButton in the inspector.
  28. /// </summary>
  29. public override void OnInspectorGUI()
  30. {
  31. IAPButton button = (IAPButton)target;
  32. serializedObject.Update();
  33. if (button.buttonType == IAPButton.ButtonType.Purchase)
  34. {
  35. EditorGUILayout.LabelField(new GUIContent("Product ID:", "Select a product from the IAP catalog."));
  36. var catalog = ProductCatalog.LoadDefaultCatalog();
  37. m_ValidIDs.Clear();
  38. m_ValidIDs.Add(kNoProduct);
  39. foreach (var product in catalog.allProducts)
  40. {
  41. m_ValidIDs.Add(product.id);
  42. }
  43. int currentIndex = string.IsNullOrEmpty(button.productId) ? 0 : m_ValidIDs.IndexOf(button.productId);
  44. int newIndex = EditorGUILayout.Popup(currentIndex, m_ValidIDs.ToArray());
  45. if (newIndex > 0 && newIndex < m_ValidIDs.Count)
  46. {
  47. m_ProductIDProperty.stringValue = m_ValidIDs[newIndex];
  48. }
  49. else
  50. {
  51. m_ProductIDProperty.stringValue = string.Empty;
  52. }
  53. if (GUILayout.Button("IAP Catalog..."))
  54. {
  55. ProductCatalogEditor.ShowWindow();
  56. }
  57. }
  58. DrawPropertiesExcluding(serializedObject, button.buttonType == IAPButton.ButtonType.Restore ? restoreButtonExcludedFields : excludedFields);
  59. serializedObject.ApplyModifiedProperties();
  60. }
  61. }
  62. }