GoogleMobileAdsSettingsEditor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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", "zh" };
  30. const string UserTrackingDescriptionEn =
  31. "This identifier will be used to deliver personalized ads to you.";
  32. const string UserTrackingDescriptionZh =
  33. "该标识符将用于向您投放个性化广告。";
  34. int selectedIndex = 0;
  35. [MenuItem("Assets/Google Mobile Ads/Settings...")]
  36. public static void OpenInspector()
  37. {
  38. Selection.activeObject = GoogleMobileAdsSettings.LoadInstance();
  39. }
  40. [MenuItem("Assets/Google Mobile Ads/Access documentation")]
  41. public static void AccessDocumentation()
  42. {
  43. Application.OpenURL("https://developers.google.com/admob/unity/quick-start");
  44. }
  45. [MenuItem("Assets/Google Mobile Ads/Raise an issue")]
  46. public static void RaiseAnIssue()
  47. {
  48. Application.OpenURL("https://support.google.com/admob/contact/contact_us_gma_sdk");
  49. }
  50. public void OnEnable()
  51. {
  52. _appIdAndroid = serializedObject.FindProperty("adMobAndroidAppId");
  53. _appIdiOS = serializedObject.FindProperty("adMobIOSAppId");
  54. _overrideDefaultGmaAndroidSdk = serializedObject.FindProperty("overrideDefaultGmaAndroidSdk");
  55. _gmaAndroidSdk = serializedObject.FindProperty("selectedGmaAndroidSdk");
  56. _enableGradleBuildPreProcessor =
  57. serializedObject.FindProperty("enableGradleBuildPreProcessor");
  58. _enableKotlinXCoroutinesPackagingOption =
  59. serializedObject.FindProperty("enableKotlinXCoroutinesPackagingOption");
  60. _disableOptimizeInitialization = serializedObject.FindProperty("disableOptimizeInitialization");
  61. _disableOptimizeAdLoading = serializedObject.FindProperty("disableOptimizeAdLoading");
  62. _userLanguage = serializedObject.FindProperty("userLanguage");
  63. _userTrackingUsageDescription =
  64. serializedObject.FindProperty("userTrackingUsageDescription");
  65. _enableExternalDependencyManager =
  66. serializedObject.FindProperty("enableExternalDependencyManager");
  67. _isInstalledViaUpm = EdmDependencyInstaller.IsInstalledViaUpm();
  68. selectedIndex = Array.IndexOf(languageCodes, _userLanguage.stringValue);
  69. selectedIndex = selectedIndex >= 0 ? selectedIndex : 0;
  70. }
  71. public override void OnInspectorGUI()
  72. {
  73. // Make sure the Settings object has all recent changes.
  74. serializedObject.Update();
  75. var settings = (GoogleMobileAdsSettings)target;
  76. if (settings == null)
  77. {
  78. UnityEngine.Debug.LogError("GoogleMobileAdsSettings is null.");
  79. return;
  80. }
  81. EditorLocalization localization = new EditorLocalization();
  82. EditorGUI.BeginChangeCheck();
  83. selectedIndex = EditorGUILayout.Popup("Language", selectedIndex, availableLanguages);
  84. if (EditorGUI.EndChangeCheck())
  85. {
  86. string code = languageCodes[selectedIndex];
  87. _userLanguage.stringValue = code;
  88. if (code == "zh")
  89. _userTrackingUsageDescription.stringValue = UserTrackingDescriptionZh;
  90. else if (code == "en")
  91. _userTrackingUsageDescription.stringValue = UserTrackingDescriptionEn;
  92. }
  93. EditorGUIUtility.labelWidth = 60.0f;
  94. EditorGUILayout.LabelField(localization.ForKey("GMA_APP_ID_LABEL"),
  95. EditorStyles.boldLabel);
  96. EditorGUI.indentLevel++;
  97. EditorGUILayout.PropertyField(_appIdAndroid, new GUIContent("Android"));
  98. EditorGUILayout.PropertyField(_appIdiOS, new GUIContent("iOS"));
  99. EditorGUILayout.HelpBox(localization.ForKey("GMA_APP_ID_HELPBOX"), MessageType.Info);
  100. EditorGUI.indentLevel--;
  101. EditorGUILayout.Separator();
  102. EditorGUIUtility.labelWidth = 325.0f;
  103. EditorGUILayout.LabelField(localization.ForKey("ANDROID_SETTINGS_LABEL"),
  104. EditorStyles.boldLabel);
  105. EditorGUI.indentLevel++;
  106. var activeArch = settings.EffectiveGmaAndroidSdk;
  107. string activeArchStr = activeArch == GoogleMobileAdsSettings.GmaAndroidSdk.Standard
  108. ? localization.ForKey("GMA_ANDROID_SDK_STANDARD")
  109. : localization.ForKey("GMA_ANDROID_SDK_NEXT_GEN");
  110. GUIStyle richLabelStyle = new GUIStyle(EditorStyles.label);
  111. richLabelStyle.richText = true;
  112. EditorGUILayout.LabelField(
  113. localization.ForKey("ACTIVE_ARCHITECTURE_LABEL") + "<b>" + activeArchStr + "</b>",
  114. richLabelStyle);
  115. EditorGUILayout.PropertyField(
  116. _overrideDefaultGmaAndroidSdk,
  117. new GUIContent(localization.ForKey("OVERRIDE_DEFAULT_GMA_ANDROID_ARCHITECTURE_SETTING")));
  118. if (!_overrideDefaultGmaAndroidSdk.boolValue)
  119. {
  120. EditorGUILayout.LabelField(localization.ForKey("OVERRIDE_DEFAULT_GMA_ANDROID_ARCHITECTURE_DESCRIPTION"), EditorStyles.wordWrappedMiniLabel);
  121. }
  122. EditorGUI.BeginDisabledGroup(!_overrideDefaultGmaAndroidSdk.boolValue);
  123. EditorGUI.indentLevel++;
  124. int currentSelected = _gmaAndroidSdk.intValue;
  125. if (!_overrideDefaultGmaAndroidSdk.boolValue)
  126. {
  127. // Force to Standard when disabled.
  128. currentSelected = (int)GoogleMobileAdsSettings.GmaAndroidSdk.Standard;
  129. }
  130. GUIStyle radioStyle = new GUIStyle(EditorStyles.radioButton);
  131. radioStyle.padding.left += 10;
  132. // Standard Radio Button
  133. EditorGUILayout.BeginHorizontal();
  134. GUILayout.Space(EditorGUI.indentLevel * 15); // Manually apply indentation
  135. if (GUILayout.Toggle(currentSelected == (int)GoogleMobileAdsSettings.GmaAndroidSdk.Standard,
  136. localization.ForKey("GMA_ANDROID_SDK_STANDARD_OPTION"), radioStyle))
  137. {
  138. currentSelected = (int)GoogleMobileAdsSettings.GmaAndroidSdk.Standard;
  139. }
  140. EditorGUILayout.EndHorizontal();
  141. // Next Gen Radio Button
  142. EditorGUILayout.BeginHorizontal();
  143. GUILayout.Space(EditorGUI.indentLevel * 15); // Manually apply indentation
  144. if (GUILayout.Toggle(currentSelected == (int)GoogleMobileAdsSettings.GmaAndroidSdk.NextGen,
  145. localization.ForKey("GMA_ANDROID_SDK_NEXT_GEN_OPTION"), radioStyle))
  146. {
  147. currentSelected = (int)GoogleMobileAdsSettings.GmaAndroidSdk.NextGen;
  148. }
  149. EditorGUILayout.EndHorizontal();
  150. if (_overrideDefaultGmaAndroidSdk.boolValue)
  151. {
  152. _gmaAndroidSdk.intValue = currentSelected;
  153. }
  154. else
  155. {
  156. _gmaAndroidSdk.intValue = (int)GoogleMobileAdsSettings.GmaAndroidSdk.Standard;
  157. }
  158. EditorGUI.indentLevel--;
  159. EditorGUI.EndDisabledGroup();
  160. EditorGUILayout.Separator();
  161. EditorGUI.BeginChangeCheck();
  162. #if ANDROID_GRADLE_BUILD_PRE_PROCESSOR_ENABLED
  163. EditorGUILayout.PropertyField(
  164. _enableGradleBuildPreProcessor,
  165. new GUIContent(
  166. localization.ForKey("ENABLE_GRADLE_BUILD_PRE_PROCESSOR_SETTING")));
  167. if (settings.EnableGradleBuildPreProcessor)
  168. {
  169. EditorGUILayout.HelpBox(
  170. localization.ForKey("ENABLE_GRADLE_BUILD_PRE_PROCESSOR_HELPBOX"),
  171. MessageType.Info);
  172. }
  173. #endif
  174. EditorGUILayout.PropertyField(
  175. _enableKotlinXCoroutinesPackagingOption,
  176. new GUIContent(
  177. localization.ForKey("ENABLE_KOTLINX_COROUTINES_PACKAGING_OPTION_SETTING")));
  178. if (settings.EnableKotlinXCoroutinesPackagingOption)
  179. {
  180. EditorGUILayout.HelpBox(
  181. localization.ForKey("ENABLE_KOTLINX_COROUTINES_PACKAGING_OPTION_HELPBOX"),
  182. MessageType.Info);
  183. }
  184. EditorGUILayout.PropertyField(
  185. _disableOptimizeInitialization,
  186. new GUIContent(localization.ForKey("DISABLE_OPTIMIZE_INITIALIZATION_SETTING")));
  187. if (settings.DisableOptimizeInitialization)
  188. {
  189. EditorGUILayout.HelpBox(localization.ForKey("DISABLE_OPTIMIZE_INITIALIZATION_HELPBOX"),
  190. MessageType.Info);
  191. }
  192. EditorGUILayout.PropertyField(
  193. _disableOptimizeAdLoading,
  194. new GUIContent(localization.ForKey("DISABLE_OPTIMIZE_AD_LOADING_SETTING")));
  195. if (settings.DisableOptimizeAdLoading)
  196. {
  197. EditorGUILayout.HelpBox(localization.ForKey("DISABLE_OPTIMIZE_AD_LOADING_HELPBOX"),
  198. MessageType.Info);
  199. }
  200. EditorGUI.indentLevel--;
  201. EditorGUILayout.Separator();
  202. bool reinstallEdm = false;
  203. if (!_isInstalledViaUpm)
  204. {
  205. EditorGUIUtility.labelWidth = 325.0f;
  206. EditorGUILayout.LabelField(localization.ForKey("EDM_SETTINGS_LABEL"),
  207. EditorStyles.boldLabel);
  208. EditorGUI.indentLevel++;
  209. bool wasEdmEnabled = _enableExternalDependencyManager.boolValue;
  210. EditorGUI.BeginDisabledGroup(wasEdmEnabled);
  211. EditorGUILayout.PropertyField(
  212. _enableExternalDependencyManager,
  213. new GUIContent(localization.ForKey("ENABLE_EDM_SETTING")));
  214. EditorGUI.EndDisabledGroup();
  215. reinstallEdm = !wasEdmEnabled && _enableExternalDependencyManager.boolValue;
  216. if (_enableExternalDependencyManager.boolValue)
  217. {
  218. EditorGUILayout.HelpBox(localization.ForKey("DISABLE_EDM_HELPBOX"),
  219. MessageType.Info);
  220. }
  221. else
  222. {
  223. EditorGUILayout.HelpBox(localization.ForKey("ENABLE_EDM_HELPBOX"),
  224. MessageType.Info);
  225. }
  226. EditorGUI.indentLevel--;
  227. EditorGUILayout.Separator();
  228. }
  229. EditorGUIUtility.labelWidth = 300.0f;
  230. EditorGUILayout.LabelField(localization.ForKey("UMP_SPECIFIC_SETTINGS_LABEL"),
  231. EditorStyles.boldLabel);
  232. EditorGUI.indentLevel++;
  233. EditorGUILayout.PropertyField(
  234. _userTrackingUsageDescription,
  235. new GUIContent(localization.ForKey("USER_TRACKING_USAGE_DESCRIPTION_SETTING")));
  236. EditorGUILayout.HelpBox(localization.ForKey("USER_TRACKING_USAGE_DESCRIPTION_HELPBOX"),
  237. MessageType.Info);
  238. EditorGUI.indentLevel--;
  239. EditorGUILayout.Separator();
  240. serializedObject.ApplyModifiedProperties();
  241. // Re-enabling the setting reinstalls EDM. Otherwise the installer would see the package
  242. // missing on the next Editor load and treat it as a publisher removal again.
  243. if (reinstallEdm)
  244. {
  245. EdmDependencyInstaller.ReinstallEdm();
  246. }
  247. }
  248. }
  249. }