Nessuna descrizione
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.

GooglePlayConfigurationSettingsBlock.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using Unity.Services.Core.Editor.OrganizationHandler;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.Purchasing
  6. {
  7. internal class GooglePlayConfigurationSettingsBlock : IPurchasingSettingsUIBlock
  8. {
  9. const string k_GooglePlayLink = "GooglePlayLink";
  10. const string k_DashboardSettingsLink = "DashboardSettingsLink";
  11. const string k_VerifiedMode = "verified-mode";
  12. const string k_UnverifiedMode = "unverified-mode";
  13. const string k_ErrorKeyFormat = "error-request-format";
  14. const string k_ErrorUnauthorized = "error-unauthorized-user";
  15. const string k_ErrorServer = "error-server-error";
  16. const string k_ErrorFetchKey = "error-fetch-key";
  17. readonly GoogleConfigurationData m_GooglePlayDataRef;
  18. readonly GoogleConfigurationWebRequests m_WebRequests;
  19. VisualElement m_ConfigurationBlock;
  20. readonly GoogleObfuscatorSection m_ObfuscatorSection;
  21. internal GooglePlayConfigurationSettingsBlock()
  22. {
  23. m_GooglePlayDataRef = GoogleConfigService.Instance().GoogleConfigData;
  24. m_WebRequests = new GoogleConfigurationWebRequests(OnGetGooglePlayKey);
  25. m_ObfuscatorSection = new GoogleObfuscatorSection(m_GooglePlayDataRef);
  26. }
  27. public VisualElement GetUIBlockElement()
  28. {
  29. return SetupConfigurationBlock();
  30. }
  31. VisualElement SetupConfigurationBlock()
  32. {
  33. m_ConfigurationBlock = SettingsUIUtils.CloneUIFromTemplate(UIResourceUtils.googlePlayConfigUxmlPath);
  34. SetupStyleSheets();
  35. PopulateConfigBlock();
  36. PopulateObfuscatorBlock();
  37. ObtainExistingGooglePlayKey();
  38. return m_ConfigurationBlock;
  39. }
  40. void SetupStyleSheets()
  41. {
  42. m_ConfigurationBlock.AddStyleSheetPath(UIResourceUtils.purchasingCommonUssPath);
  43. m_ConfigurationBlock.AddStyleSheetPath(EditorGUIUtility.isProSkin ? UIResourceUtils.purchasingDarkUssPath : UIResourceUtils.purchasingLightUssPath);
  44. }
  45. void PopulateConfigBlock()
  46. {
  47. ToggleGoogleKeyStateDisplay();
  48. SetupLinkActions();
  49. }
  50. void PopulateObfuscatorBlock()
  51. {
  52. m_ObfuscatorSection.SetupObfuscatorBlock(m_ConfigurationBlock);
  53. m_ObfuscatorSection.RegisterGooglePlayKeyChangedCallback();
  54. }
  55. void ObtainExistingGooglePlayKey()
  56. {
  57. if (m_GooglePlayDataRef.revenueTrackingState != GooglePlayRevenueTrackingKeyState.Verified)
  58. {
  59. m_WebRequests.RequestRetrieveKeyOperation();
  60. }
  61. else
  62. {
  63. SetGooglePlayKeyText(m_GooglePlayDataRef.googlePlayKey);
  64. ToggleGoogleKeyStateDisplay();
  65. }
  66. }
  67. void SetupLinkActions()
  68. {
  69. var googlePlayExternalLink = m_ConfigurationBlock.Q(k_GooglePlayLink);
  70. if (googlePlayExternalLink != null)
  71. {
  72. var clickable = new Clickable(OpenGooglePlayDevConsole);
  73. googlePlayExternalLink.AddManipulator(clickable);
  74. }
  75. var projectSettingsDashboardLink = m_ConfigurationBlock.Q(k_DashboardSettingsLink);
  76. if (projectSettingsDashboardLink != null)
  77. {
  78. var clickable = new Clickable(OpenProjectSettingsUnityDashboard);
  79. projectSettingsDashboardLink.AddManipulator(clickable);
  80. }
  81. }
  82. static void OpenGooglePlayDevConsole()
  83. {
  84. Application.OpenURL(PurchasingUrls.googlePlayDevConsoleUrl);
  85. }
  86. static void OpenProjectSettingsUnityDashboard()
  87. {
  88. Application.OpenURL(BuildProjectSettingsUri());
  89. GameServicesEventSenderHelpers.SendProjectSettingsOpenDashboardForPublicKey();
  90. }
  91. static string BuildProjectSettingsUri()
  92. {
  93. return string.Format(PurchasingUrls.protjectSettingUrl, OrganizationProvider.Organization.Key, CloudProjectSettings.projectId);
  94. }
  95. void ToggleGoogleKeyStateDisplay()
  96. {
  97. ToggleVerifiedModeDisplay();
  98. ToggleUnverifiedModeDisplay();
  99. }
  100. GooglePlayRevenueTrackingKeyState GetTrackingKeyState()
  101. {
  102. return m_GooglePlayDataRef.revenueTrackingState;
  103. }
  104. void ToggleVerifiedModeDisplay()
  105. {
  106. var verifiedMode = m_ConfigurationBlock.Q(k_VerifiedMode);
  107. if (verifiedMode != null)
  108. {
  109. verifiedMode.style.display = GetTrackingKeyState() == GooglePlayRevenueTrackingKeyState.Verified ? DisplayStyle.Flex : (StyleEnum<DisplayStyle>)DisplayStyle.None;
  110. }
  111. }
  112. void ToggleUnverifiedModeDisplay()
  113. {
  114. var unVerifiedMode = m_ConfigurationBlock.Q(k_UnverifiedMode);
  115. if (unVerifiedMode != null)
  116. {
  117. unVerifiedMode.style.display = (GetTrackingKeyState() == GooglePlayRevenueTrackingKeyState.Verified)
  118. ? DisplayStyle.None
  119. : DisplayStyle.Flex;
  120. ToggleErrorStateBlockVisibility(GooglePlayRevenueTrackingKeyState.InvalidFormat, k_ErrorKeyFormat);
  121. ToggleErrorStateBlockVisibility(GooglePlayRevenueTrackingKeyState.UnauthorizedUser, k_ErrorUnauthorized);
  122. ToggleErrorStateBlockVisibility(GooglePlayRevenueTrackingKeyState.ServerError, k_ErrorServer);
  123. ToggleErrorStateBlockVisibility(GooglePlayRevenueTrackingKeyState.CantFetch, k_ErrorFetchKey);
  124. }
  125. }
  126. void ToggleErrorStateBlockVisibility(GooglePlayRevenueTrackingKeyState matchingBlockState, string blockName)
  127. {
  128. var errorSection = m_ConfigurationBlock.Q(blockName);
  129. if (errorSection != null)
  130. {
  131. errorSection.style.display = (GetTrackingKeyState() == matchingBlockState)
  132. ? DisplayStyle.Flex
  133. : DisplayStyle.None;
  134. }
  135. }
  136. void OnGetGooglePlayKey(string key, GooglePlayRevenueTrackingKeyState state)
  137. {
  138. m_GooglePlayDataRef.googlePlayKey = key;
  139. m_GooglePlayDataRef.revenueTrackingState = state;
  140. if (!string.IsNullOrEmpty(key))
  141. {
  142. SetGooglePlayKeyText(key);
  143. }
  144. ToggleGoogleKeyStateDisplay();
  145. }
  146. void SetGooglePlayKeyText(string key)
  147. {
  148. m_ObfuscatorSection.SetGooglePlayKeyText(key);
  149. }
  150. }
  151. }