UdpInstaller.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor.Callbacks;
  5. using UnityEditorInternal;
  6. using UnityEngine;
  7. using UnityEngine.Purchasing;
  8. #if UNITY_2019_3_OR_NEWER
  9. using UnityEditor.PackageManager;
  10. using UnityEditor.PackageManager.UI;
  11. #endif
  12. namespace UnityEditor.Purchasing
  13. {
  14. /// <summary>
  15. /// This class directs the developer to install UDP if it is not already installed through Package Manager.
  16. /// </summary>
  17. public class UdpInstaller
  18. {
  19. #if UNITY_2019_3_OR_NEWER
  20. private const string k_PackManWindowTitle = "Get UDP via Package Manager";
  21. #else
  22. private const string k_AssetStoreWindowTitle = "Get UDP via Asset Store";
  23. #endif
  24. private static readonly Vector2 k_WindowDims = new Vector2(480, 120);
  25. internal static void PromptUdpInstallation()
  26. {
  27. var window = (UdpInstallInstructionsWindow)EditorWindow.GetWindow(typeof(UdpInstallInstructionsWindow));
  28. #if UNITY_2019_3_OR_NEWER
  29. window.titleContent.text = k_PackManWindowTitle;
  30. #else
  31. window.titleContent.text = k_AssetStoreWindowTitle;
  32. #endif
  33. window.minSize = k_WindowDims;
  34. window.Show();
  35. }
  36. }
  37. /// <summary>
  38. /// Instructs user how to install UDP.
  39. /// </summary>
  40. internal class UdpInstallInstructionsWindow : RichEditorWindow
  41. {
  42. #if UNITY_2019_3_OR_NEWER
  43. private const string k_InfoText = "In order to use this functionality, you must install or update the Unity Distribution Portal Package. Would you like to begin?";
  44. private const string k_UdpPackageName = "com.unity.purchasing.udp";
  45. #else
  46. private const string k_InfoText = "In order to use this functionality, you must install or update the Unity Distribution Portal Plugin. Would you like to begin?";
  47. //Browser URL is "https://assetstore.unity.com/packages/add-ons/services/billing/unity-distribution-portal-138507".
  48. //Took the number at the end and dropped it into the pattern "content/{0}?assetID={1}".
  49. //This special URL fragment is required by the API and appended to the root URL pattern
  50. private const string k_UdpAssetStoreIdentifier = "content/138507?assetID=138507";
  51. #endif
  52. private const string k_NotNowButtonText = "Not Now";
  53. private const string k_GoButtonText = "Go";
  54. private void OnGUI()
  55. {
  56. // Make fit entire window vertically
  57. EditorGUILayout.BeginVertical(GUILayout.ExpandHeight(true));
  58. // Align info text & values horizontally
  59. EditorGUILayout.BeginHorizontal();
  60. // Version labels - horizontal stack
  61. EditorGUILayout.BeginVertical();
  62. GUILayout.Label(k_InfoText, EditorStyles.wordWrappedLabel);
  63. EditorGUILayout.EndVertical();
  64. EditorGUILayout.EndHorizontal(); // END Info text row
  65. // Action button row
  66. EditorGUILayout.BeginHorizontal();
  67. GUILayout.FlexibleSpace();
  68. var notNowButton = GUILayout.Button(k_NotNowButtonText);
  69. var goButton = GUILayout.Button(k_GoButtonText);
  70. EditorGUILayout.EndHorizontal(); // END Action button row
  71. EditorGUILayout.EndVertical(); // END window alignment
  72. // Handle buttons
  73. if (notNowButton)
  74. {
  75. Close();
  76. }
  77. if (goButton)
  78. {
  79. // Direct user to install page. User must install manually. Close immediately
  80. GoToInstaller();
  81. Close();
  82. }
  83. }
  84. void GoToInstaller()
  85. {
  86. #if UNITY_2019_3_OR_NEWER
  87. PackageManager.UI.Window.Open(k_UdpPackageName);
  88. #else
  89. AssetStore.Open(k_UdpAssetStoreIdentifier);
  90. #endif
  91. }
  92. }
  93. }