Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AbstractIAPButtonEditor.cs 3.2KB

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