IapButtonMenu.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. using UnityEngine.Purchasing;
  3. namespace UnityEditor.Purchasing
  4. {
  5. /// <summary>
  6. /// IAPButtonMenu class creates options in menus to create the <see cref="IAPButton"/>.
  7. /// </summary>
  8. public static class IAPButtonMenu
  9. {
  10. /// <summary>
  11. /// Add option to create a IAPButton from the GameObject menu.
  12. /// </summary>
  13. [MenuItem("GameObject/" + IapMenuConsts.PurchasingDisplayName + "/IAP Button", false, 10)]
  14. public static void GameObjectCreateUnityIAPButton()
  15. {
  16. CreateUnityIAPButtonInternal();
  17. GenericEditorMenuItemClickEventSenderHelpers.SendGameObjectMenuAddIapButtonEvent();
  18. }
  19. /// <summary>
  20. /// Add option to create a IAPButton from the Window/UnityIAP menu.
  21. /// </summary>
  22. [MenuItem(IapMenuConsts.MenuItemRoot + "/Create IAP Button", false, 100)]
  23. public static void CreateUnityIAPButton()
  24. {
  25. CreateUnityIAPButtonInternal();
  26. GenericEditorMenuItemClickEventSenderHelpers.SendIapMenuAddIapButtonEvent();
  27. GameServicesEventSenderHelpers.SendTopMenuCreateIapButtonEvent();
  28. }
  29. static void CreateUnityIAPButtonInternal()
  30. {
  31. GameObject buttonObject = CreateButtonObject();
  32. if (buttonObject)
  33. {
  34. IAPButton iapButton = buttonObject.AddComponent<IAPButton>();
  35. if (iapButton != null)
  36. {
  37. UnityEditorInternal.ComponentUtility.MoveComponentUp(iapButton);
  38. UnityEditorInternal.ComponentUtility.MoveComponentUp(iapButton);
  39. UnityEditorInternal.ComponentUtility.MoveComponentUp(iapButton);
  40. }
  41. }
  42. }
  43. static GameObject CreateButtonObject()
  44. {
  45. ExecuteButtonMenuItem();
  46. return Selection.activeGameObject;
  47. }
  48. static void ExecuteButtonMenuItem()
  49. {
  50. #if UNITY_2022_1_OR_NEWER || (UNITY_2021_2_OR_NEWER && !(UNITY_2021_2_2 || UNITY_2021_2_1))
  51. EditorApplication.ExecuteMenuItem("GameObject/UI/Legacy/Button");
  52. #else
  53. EditorApplication.ExecuteMenuItem("GameObject/UI/Button");
  54. #endif
  55. }
  56. }
  57. }