You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
3.5 KiB

  1. #if UNITY_PURCHASING
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.Purchasing;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. namespace UnityEditor.Purchasing
  8. {
  9. public static class IAPButtonMenu
  10. {
  11. [MenuItem("GameObject/Unity IAP/IAP Button", false, 10)]
  12. public static void GameObjectCreateUnityIAPButton()
  13. {
  14. CreateUnityIAPButton();
  15. }
  16. [MenuItem ("Window/Unity IAP/Create IAP Button", false, 5)]
  17. public static void CreateUnityIAPButton()
  18. {
  19. // Create Button
  20. EditorApplication.ExecuteMenuItem("GameObject/UI/Button");
  21. // Get GameObject of Button
  22. GameObject gO = Selection.activeGameObject;
  23. // Add IAP Button component to GameObject
  24. IAPButton iapButton = null;
  25. if (gO) {
  26. iapButton = gO.AddComponent<IAPButton>();
  27. }
  28. if (iapButton != null) {
  29. UnityEditorInternal.ComponentUtility.MoveComponentUp(iapButton);
  30. UnityEditorInternal.ComponentUtility.MoveComponentUp(iapButton);
  31. UnityEditorInternal.ComponentUtility.MoveComponentUp(iapButton);
  32. }
  33. }
  34. }
  35. public static class IAPListenerMenu
  36. {
  37. [MenuItem("GameObject/Unity IAP/IAP Listener", false, 10)]
  38. public static void GameObjectCreateUnityIAPListener()
  39. {
  40. CreateUnityIAPListener();
  41. }
  42. [MenuItem ("Window/Unity IAP/Create IAP Listener", false, 6)]
  43. public static void CreateUnityIAPListener()
  44. {
  45. // Create empty GameObject
  46. EditorApplication.ExecuteMenuItem("GameObject/Create Empty");
  47. // Get GameObject
  48. GameObject gO = Selection.activeGameObject;
  49. // Add IAP Listener component to GameObject
  50. if (gO) {
  51. gO.AddComponent<IAPListener>();
  52. gO.name = "IAP Listener";
  53. }
  54. }
  55. }
  56. [CustomEditor(typeof(IAPButton))]
  57. [CanEditMultipleObjects]
  58. public class IAPButtonEditor : Editor
  59. {
  60. private static readonly string[] excludedFields = new string[] { "m_Script" };
  61. private static readonly string[] restoreButtonExcludedFields = new string[] { "m_Script", "consumePurchase", "onPurchaseComplete", "onPurchaseFailed", "titleText", "descriptionText", "priceText" };
  62. private const string kNoProduct = "<None>";
  63. private List<string> m_ValidIDs = new List<string>();
  64. private SerializedProperty m_ProductIDProperty;
  65. public void OnEnable()
  66. {
  67. m_ProductIDProperty = serializedObject.FindProperty("productId");
  68. }
  69. public override void OnInspectorGUI()
  70. {
  71. IAPButton button = (IAPButton)target;
  72. serializedObject.Update();
  73. if (button.buttonType == IAPButton.ButtonType.Purchase) {
  74. EditorGUILayout.LabelField(new GUIContent("Product ID:", "Select a product from the IAP catalog"));
  75. var catalog = ProductCatalog.LoadDefaultCatalog();
  76. m_ValidIDs.Clear();
  77. m_ValidIDs.Add(kNoProduct);
  78. foreach (var product in catalog.allProducts) {
  79. m_ValidIDs.Add(product.id);
  80. }
  81. int currentIndex = string.IsNullOrEmpty(button.productId) ? 0 : m_ValidIDs.IndexOf(button.productId);
  82. int newIndex = EditorGUILayout.Popup(currentIndex, m_ValidIDs.ToArray());
  83. if (newIndex > 0 && newIndex < m_ValidIDs.Count) {
  84. m_ProductIDProperty.stringValue = m_ValidIDs[newIndex];
  85. } else {
  86. m_ProductIDProperty.stringValue = string.Empty;
  87. }
  88. if (GUILayout.Button("IAP Catalog...")) {
  89. ProductCatalogEditor.ShowWindow();
  90. }
  91. }
  92. DrawPropertiesExcluding(serializedObject, button.buttonType == IAPButton.ButtonType.Restore ? restoreButtonExcludedFields : excludedFields);
  93. serializedObject.ApplyModifiedProperties();
  94. }
  95. }
  96. }
  97. #endif