GoogleMobileAdsSettingsEditor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #if UNITY_6000_0_OR_NEWER || UNITY_2023 || UNITY_2022 || UNITY_2021_3_55 || UNITY_2021_3_54 || UNITY_2021_3_53 || UNITY_2021_3_52 || UNITY_2021_3_51 || UNITY_2021_3_50 || UNITY_2021_3_49 || UNITY_2021_3_48 || UNITY_2021_3_47 || UNITY_2021_3_46 || UNITY_2021_3_45 || UNITY_2021_3_44 || UNITY_2021_3_43 || UNITY_2021_3_42 || UNITY_2021_3_41
  2. #define ANDROID_GRADLE_BUILD_PRE_PROCESSOR_ENABLED
  3. #endif
  4. using System;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace GoogleMobileAds.Editor
  8. {
  9. [InitializeOnLoad]
  10. [CustomEditor(typeof(GoogleMobileAdsSettings))]
  11. public class GoogleMobileAdsSettingsEditor : UnityEditor.Editor
  12. {
  13. SerializedProperty _appIdAndroid;
  14. SerializedProperty _appIdiOS;
  15. SerializedProperty _enableGradleBuildPreProcessor;
  16. SerializedProperty _enableKotlinXCoroutinesPackagingOption;
  17. SerializedProperty _disableOptimizeInitialization;
  18. SerializedProperty _disableOptimizeAdLoading;
  19. SerializedProperty _userLanguage;
  20. SerializedProperty _userTrackingUsageDescription;
  21. SerializedProperty _overrideDefaultGmaAndroidSdk;
  22. SerializedProperty _gmaAndroidSdk;
  23. SerializedProperty _enableExternalDependencyManager;
  24. bool _isInstalledViaUpm;
  25. // Using an ordered list of languages is computationally expensive when trying to create an
  26. // array out of them for purposes of showing a dropdown menu. Care should be taken to ensure
  27. // these arrays are kept in sync.
  28. string[] availableLanguages = new string[] { "English", "French"};
  29. string[] languageCodes = new string[] { "en", "fr" };
  30. int selectedIndex = 0;
  31. [MenuItem("Assets/Google Mobile Ads/Settings...")]
  32. public static void OpenInspector()
  33. {
  34. Selection.activeObject = GoogleMobileAdsSettings.LoadInstance();
  35. }
  36. [MenuItem("Assets/Google Mobile Ads/Access documentation")]
  37. public static void AccessDocumentation()
  38. {
  39. Application.OpenURL("https://developers.google.com/admob/unity/quick-start");
  40. }
  41. [MenuItem("Assets/Google Mobile Ads/Raise an issue")]
  42. public static void RaiseAnIssue()
  43. {
  44. Application.OpenURL("https://support.google.com/admob/contact/contact_us_gma_sdk");
  45. }
  46. public void OnEnable()
  47. {
  48. _appIdAndroid = serializedObject.FindProperty("adMobAndroidAppId");
  49. _appIdiOS = serializedObject.FindProperty("adMobIOSAppId");
  50. _overrideDefaultGmaAndroidSdk = serializedObject.FindProperty("overrideDefaultGmaAndroidSdk");
  51. _gmaAndroidSdk = serializedObject.FindProperty("selectedGmaAndroidSdk");
  52. _enableGradleBuildPreProcessor =
  53. serializedObject.FindProperty("enableGradleBuildPreProcessor");
  54. _enableKotlinXCoroutinesPackagingOption =
  55. serializedObject.FindProperty("enableKotlinXCoroutinesPackagingOption");
  56. _disableOptimizeInitialization = serializedObject.FindProperty("disableOptimizeInitialization");
  57. _disableOptimizeAdLoading = serializedObject.FindProperty("disableOptimizeAdLoading");
  58. _userLanguage = serializedObject.FindProperty("userLanguage");
  59. _userTrackingUsageDescription =
  60. serializedObject.FindProperty("userTrackingUsageDescription");
  61. _enableExternalDependencyManager =
  62. serializedObject.FindProperty("enableExternalDependencyManager");
  63. _isInstalledViaUpm = EdmDependencyInstaller.IsInstalledViaUpm();
  64. selectedIndex = Array.IndexOf(languageCodes, _userLanguage.stringValue);
  65. selectedIndex = selectedIndex >= 0 ? selectedIndex : 0;
  66. }
  67. public override void OnInspectorGUI()
  68. {
  69. // Make sure the Settings object has all recent changes.
  70. serializedObject.Update();
  71. var settings = (GoogleMobileAdsSettings)target;
  72. if (settings == null)
  73. {
  74. UnityEngine.Debug.LogError("GoogleMobileAdsSettings is null.");
  75. return;
  76. }
  77. EditorLocalization localization = new EditorLocalization();
  78. EditorGUI.BeginChangeCheck();
  79. selectedIndex = EditorGUILayout.Popup("Language", selectedIndex, availableLanguages);
  80. if (EditorGUI.EndChangeCheck())
  81. {
  82. _userLanguage.stringValue = languageCodes[selectedIndex];
  83. }
  84. EditorGUIUtility.labelWidth = 60.0f;
  85. EditorGUILayout.LabelField(localization.ForKey("GMA_APP_ID_LABEL"),
  86. EditorStyles.boldLabel);
  87. EditorGUI.indentLevel++;
  88. EditorGUILayout.PropertyField(_appIdAndroid, new GUIContent("Android"));
  89. EditorGUILayout.PropertyField(_appIdiOS, new GUIContent("iOS"));
  90. EditorGUILayout.HelpBox(localization.ForKey("GMA_APP_ID_HELPBOX"), MessageType.Info);
  91. EditorGUI.indentLevel--;
  92. EditorGUILayout.Separator();
  93. EditorGUIUtility.labelWidth = 325.0f;
  94. EditorGUILayout.LabelField(localization.ForKey("ANDROID_SETTINGS_LABEL"),
  95. EditorStyles.boldLabel);
  96. EditorGUI.indentLevel++;
  97. var activeArch = settings.EffectiveGmaAndroidSdk;
  98. string activeArchStr = activeArch == GoogleMobileAdsSettings.GmaAndroidSdk.Standard
  99. ? localization.ForKey("GMA_ANDROID_SDK_STANDARD")
  100. : localization.ForKey("GMA_ANDROID_SDK_NEXT_GEN");
  101. GUIStyle richLabelStyle = new GUIStyle(EditorStyles.label);
  102. richLabelStyle.richText = true;
  103. EditorGUILayout.LabelField(
  104. localization.ForKey("ACTIVE_ARCHITECTURE_LABEL") + "<b>" + activeArchStr + "</b>",
  105. richLabelStyle);
  106. EditorGUILayout.PropertyField(
  107. _overrideDefaultGmaAndroidSdk,
  108. new GUIContent(localization.ForKey("OVERRIDE_DEFAULT_GMA_ANDROID_ARCHITECTURE_SETTING")));
  109. if (!_overrideDefaultGmaAndroidSdk.boolValue)
  110. {
  111. EditorGUILayout.LabelField(localization.ForKey("OVERRIDE_DEFAULT_GMA_ANDROID_ARCHITECTURE_DESCRIPTION"), EditorStyles.wordWrappedMiniLabel);
  112. }
  113. EditorGUI.BeginDisabledGroup(!_overrideDefaultGmaAndroidSdk.boolValue);
  114. EditorGUI.indentLevel++;
  115. int currentSelected = _gmaAndroidSdk.intValue;
  116. if (!_overrideDefaultGmaAndroidSdk.boolValue)
  117. {
  118. // Force to Standard when disabled.
  119. currentSelected = (int)GoogleMobileAdsSettings.GmaAndroidSdk.Standard;
  120. }
  121. GUIStyle radioStyle = new GUIStyle(EditorStyles.radioButton);
  122. radioStyle.padding.left += 10;
  123. // Standard Radio Button
  124. EditorGUILayout.BeginHorizontal();
  125. GUILayout.Space(EditorGUI.indentLevel * 15); // Manually apply indentation
  126. if (GUILayout.Toggle(currentSelected == (int)GoogleMobileAdsSettings.GmaAndroidSdk.Standard,
  127. localization.ForKey("GMA_ANDROID_SDK_STANDARD_OPTION"), radioStyle))
  128. {
  129. currentSelected = (int)GoogleMobileAdsSettings.GmaAndroidSdk.Standard;
  130. }
  131. EditorGUILayout.EndHorizontal();
  132. // Next Gen Radio Button
  133. EditorGUILayout.BeginHorizontal();
  134. GUILayout.Space(EditorGUI.indentLevel * 15); // Manually apply indentation
  135. if (GUILayout.Toggle(currentSelected == (int)GoogleMobileAdsSettings.GmaAndroidSdk.NextGen,
  136. localization.ForKey("GMA_ANDROID_SDK_NEXT_GEN_OPTION"), radioStyle))
  137. {
  138. currentSelected = (int)GoogleMobileAdsSettings.GmaAndroidSdk.NextGen;
  139. }
  140. EditorGUILayout.EndHorizontal();
  141. if (_overrideDefaultGmaAndroidSdk.boolValue)
  142. {
  143. _gmaAndroidSdk.intValue = currentSelected;
  144. }
  145. else
  146. {
  147. _gmaAndroidSdk.intValue = (int)GoogleMobileAdsSettings.GmaAndroidSdk.Standard;
  148. }
  149. EditorGUI.indentLevel--;
  150. EditorGUI.EndDisabledGroup();
  151. EditorGUILayout.Separator();
  152. EditorGUI.BeginChangeCheck();
  153. #if ANDROID_GRADLE_BUILD_PRE_PROCESSOR_ENABLED
  154. EditorGUILayout.PropertyField(
  155. _enableGradleBuildPreProcessor,
  156. new GUIContent(
  157. localization.ForKey("ENABLE_GRADLE_BUILD_PRE_PROCESSOR_SETTING")));
  158. if (settings.EnableGradleBuildPreProcessor)
  159. {
  160. EditorGUILayout.HelpBox(
  161. localization.ForKey("ENABLE_GRADLE_BUILD_PRE_PROCESSOR_HELPBOX"),
  162. MessageType.Info);
  163. }
  164. #endif
  165. EditorGUILayout.PropertyField(
  166. _enableKotlinXCoroutinesPackagingOption,
  167. new GUIContent(
  168. localization.ForKey("ENABLE_KOTLINX_COROUTINES_PACKAGING_OPTION_SETTING")));
  169. if (settings.EnableKotlinXCoroutinesPackagingOption)
  170. {
  171. EditorGUILayout.HelpBox(
  172. localization.ForKey("ENABLE_KOTLINX_COROUTINES_PACKAGING_OPTION_HELPBOX"),
  173. MessageType.Info);
  174. }
  175. EditorGUILayout.PropertyField(
  176. _disableOptimizeInitialization,
  177. new GUIContent(localization.ForKey("DISABLE_OPTIMIZE_INITIALIZATION_SETTING")));
  178. if (settings.DisableOptimizeInitialization)
  179. {
  180. EditorGUILayout.HelpBox(localization.ForKey("DISABLE_OPTIMIZE_INITIALIZATION_HELPBOX"),
  181. MessageType.Info);
  182. }
  183. EditorGUILayout.PropertyField(
  184. _disableOptimizeAdLoading,
  185. new GUIContent(localization.ForKey("DISABLE_OPTIMIZE_AD_LOADING_SETTING")));
  186. if (settings.DisableOptimizeAdLoading)
  187. {
  188. EditorGUILayout.HelpBox(localization.ForKey("DISABLE_OPTIMIZE_AD_LOADING_HELPBOX"),
  189. MessageType.Info);
  190. }
  191. EditorGUI.indentLevel--;
  192. EditorGUILayout.Separator();
  193. bool reinstallEdm = false;
  194. if (!_isInstalledViaUpm)
  195. {
  196. EditorGUIUtility.labelWidth = 325.0f;
  197. EditorGUILayout.LabelField(localization.ForKey("EDM_SETTINGS_LABEL"),
  198. EditorStyles.boldLabel);
  199. EditorGUI.indentLevel++;
  200. bool wasEdmEnabled = _enableExternalDependencyManager.boolValue;
  201. EditorGUI.BeginDisabledGroup(wasEdmEnabled);
  202. EditorGUILayout.PropertyField(
  203. _enableExternalDependencyManager,
  204. new GUIContent(localization.ForKey("ENABLE_EDM_SETTING")));
  205. EditorGUI.EndDisabledGroup();
  206. reinstallEdm = !wasEdmEnabled && _enableExternalDependencyManager.boolValue;
  207. if (_enableExternalDependencyManager.boolValue)
  208. {
  209. EditorGUILayout.HelpBox(localization.ForKey("DISABLE_EDM_HELPBOX"),
  210. MessageType.Info);
  211. }
  212. else
  213. {
  214. EditorGUILayout.HelpBox(localization.ForKey("ENABLE_EDM_HELPBOX"),
  215. MessageType.Info);
  216. }
  217. EditorGUI.indentLevel--;
  218. EditorGUILayout.Separator();
  219. }
  220. EditorGUIUtility.labelWidth = 300.0f;
  221. EditorGUILayout.LabelField(localization.ForKey("UMP_SPECIFIC_SETTINGS_LABEL"),
  222. EditorStyles.boldLabel);
  223. EditorGUI.indentLevel++;
  224. EditorGUILayout.PropertyField(
  225. _userTrackingUsageDescription,
  226. new GUIContent(localization.ForKey("USER_TRACKING_USAGE_DESCRIPTION_SETTING")));
  227. EditorGUILayout.HelpBox(localization.ForKey("USER_TRACKING_USAGE_DESCRIPTION_HELPBOX"),
  228. MessageType.Info);
  229. EditorGUI.indentLevel--;
  230. EditorGUILayout.Separator();
  231. serializedObject.ApplyModifiedProperties();
  232. // Re-enabling the setting reinstalls EDM. Otherwise the installer would see the package
  233. // missing on the next Editor load and treat it as a publisher removal again.
  234. if (reinstallEdm)
  235. {
  236. EdmDependencyInstaller.ReinstallEdm();
  237. }
  238. }
  239. }
  240. }