AdsSettingsProvider.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #if SERVICES_SDK_CORE_ENABLED
  2. using System.Collections.Generic;
  3. using Unity.Services.Core.Editor;
  4. using UnityEditor;
  5. using UnityEngine.UIElements;
  6. namespace UnityEngine.Advertisements.Editor
  7. {
  8. class AdsSettingsProvider : EditorGameServiceSettingsProvider
  9. {
  10. VisualElement m_Root;
  11. GettingStartedUi m_GettingStartedUi;
  12. TestModeUi m_TestModeUi;
  13. GameIdsUi m_GameIdsUi;
  14. VisualElement m_SupportedPlatformsUi;
  15. bool m_HasLoggedAssetStorePackageInstalled;
  16. public AdsSettingsProvider(SettingsScope scopes, IEnumerable<string> keywords = null)
  17. : base(GetSettingsPath(), scopes, keywords) {}
  18. [SettingsProvider]
  19. public static SettingsProvider CreateSettingsProvider()
  20. {
  21. #if ENABLE_EDITOR_GAME_SERVICES
  22. return new AdsSettingsProvider(SettingsScope.Project);
  23. #else
  24. return null;
  25. #endif
  26. }
  27. internal static string GetSettingsPath()
  28. {
  29. return GenerateProjectSettingsPath(new AdsServiceIdentifier().GetKey());
  30. }
  31. AdsService m_Service = (AdsService)EditorGameServiceRegistry.Instance.GetEditorGameService<AdsServiceIdentifier>();
  32. protected override IEditorGameService EditorGameService => m_Service;
  33. protected override string Title => UiConstants.LocalizedStrings.Ads;
  34. protected override string Description => UiConstants.LocalizedStrings.Description;
  35. protected override VisualElement GenerateServiceDetailUI()
  36. {
  37. m_Root = new VisualElement();
  38. m_GettingStartedUi = new GettingStartedUi();
  39. m_TestModeUi = new TestModeUi();
  40. m_GameIdsUi = new GameIdsUi();
  41. m_SupportedPlatformsUi = PlatformSupportUiHelper.GeneratePlatformSupport(UiConstants.SupportedPlatforms);
  42. SetUpStyles();
  43. RefreshDetailUI();
  44. return m_Root;
  45. }
  46. void SetUpStyles()
  47. {
  48. m_Root.AddToClassList(UiConstants.ClassNames.Ads);
  49. var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(UiConstants.StyleSheetPaths.Common);
  50. if (!(styleSheet is null))
  51. {
  52. m_Root.styleSheets.Add(styleSheet);
  53. }
  54. }
  55. void RefreshDetailUI()
  56. {
  57. if (m_Root is null)
  58. {
  59. return;
  60. }
  61. m_Root.Clear();
  62. m_Root.Add(m_GettingStartedUi);
  63. if (m_Service.Enabler.IsEnabled())
  64. {
  65. m_Root.Add(m_TestModeUi);
  66. m_Root.Add(m_GameIdsUi);
  67. LogOnceAssetStorePackageInstalled();
  68. }
  69. m_Root.Add(m_SupportedPlatformsUi);
  70. TranslateAllLabelsIn(m_Root);
  71. }
  72. void LogOnceAssetStorePackageInstalled()
  73. {
  74. if (m_HasLoggedAssetStorePackageInstalled
  75. || !PluginUtils.AreAssetStorePluginsInstalled())
  76. {
  77. return;
  78. }
  79. Debug.LogWarning(UiConstants.LocalizedStrings.AssetStorePackageInstalledMessage);
  80. m_HasLoggedAssetStorePackageInstalled = true;
  81. }
  82. static void TranslateAllLabelsIn(VisualElement root)
  83. {
  84. root.Query<TextElement>()
  85. .ForEach(TranslateLabel);
  86. string TranslateLabel(TextElement label)
  87. {
  88. label.text = L10n.Tr(label.text);
  89. return label.text;
  90. }
  91. }
  92. public override void OnActivate(string searchContext, VisualElement rootElement)
  93. {
  94. base.OnActivate(searchContext, rootElement);
  95. m_Service.GameIdsUpdated += OnGameIdsUpdated;
  96. var serviceEnabler = (AdsServiceEnabler)m_Service.Enabler;
  97. serviceEnabler.ServiceEnabled += RefreshDetailUI;
  98. serviceEnabler.ServiceDisabled += RefreshDetailUI;
  99. }
  100. public override void OnDeactivate()
  101. {
  102. base.OnDeactivate();
  103. m_Service.GameIdsUpdated -= OnGameIdsUpdated;
  104. var serviceEnabler = (AdsServiceEnabler)m_Service.Enabler;
  105. serviceEnabler.ServiceEnabled -= RefreshDetailUI;
  106. serviceEnabler.ServiceDisabled -= RefreshDetailUI;
  107. }
  108. void OnGameIdsUpdated()
  109. {
  110. if (m_GameIdsUi is null)
  111. {
  112. return;
  113. }
  114. m_GameIdsUi.RefreshGameIds();
  115. Repaint();
  116. }
  117. }
  118. }
  119. #endif