MaxSdkUnityEditor.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. // This script is used for Unity Editor and non Android or iOS platforms.
  2. #if UNITY_EDITOR || !(UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS)
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using AppLovinMax.Internal;
  8. using AppLovinMax.ThirdParty.MiniJson;
  9. #if UNITY_EDITOR
  10. using UnityEditor;
  11. #endif
  12. using UnityEngine;
  13. using UnityEngine.UI;
  14. using Object = UnityEngine.Object;
  15. /// <summary>
  16. /// Unity Editor AppLovin MAX Unity Plugin implementation
  17. /// </summary>
  18. public class MaxSdkUnityEditor : MaxSdkBase
  19. {
  20. private static bool _isInitialized;
  21. private static bool _hasUserConsent = false;
  22. private static bool _isUserConsentSet = false;
  23. private static bool _doNotSell = false;
  24. private static bool _isDoNotSellSet = false;
  25. private static bool _showStubAds = true;
  26. private static SafeAreaInsets _safeAreaInsets = new SafeAreaInsets(new int[] {0, 0, 0, 0});
  27. private static readonly HashSet<string> RequestedAdUnits = new HashSet<string>();
  28. private static readonly HashSet<string> ReadyAdUnits = new HashSet<string>();
  29. private static readonly Dictionary<string, GameObject> StubBanners = new Dictionary<string, GameObject>();
  30. // Ad Placement
  31. private static readonly Dictionary<string, object> BannerPlacements = new Dictionary<string, object>();
  32. private static readonly Dictionary<string, object> MRecPlacements = new Dictionary<string, object>();
  33. [RuntimeInitializeOnLoadMethod]
  34. public static void InitializeMaxSdkUnityEditorOnLoad()
  35. {
  36. // Unity destroys the stub banners each time the editor exits play mode, but the StubBanners stays in memory if Enter Play Mode settings is enabled.
  37. StubBanners.Clear();
  38. }
  39. #region Initialization
  40. /// <summary>
  41. /// Initialize the default instance of AppLovin SDK.
  42. ///
  43. /// Please make sure that application's Android manifest or Info.plist includes the AppLovin SDK key.
  44. ///
  45. /// <param name="adUnitIds">
  46. /// OPTIONAL: Set the MAX ad unit ids to be used for this instance of the SDK. 3rd-party SDKs will be initialized with the credentials configured for these ad unit ids.
  47. /// This should only be used if you have different sets of ad unit ids / credentials for the same package name.</param>
  48. /// </summary>
  49. public static void InitializeSdk(string[] adUnitIds = null)
  50. {
  51. _isInitialized = true;
  52. // Slight delay to emulate the SDK initializing
  53. ExecuteWithDelay(0.1f, () =>
  54. {
  55. _isInitialized = true;
  56. #if UNITY_EDITOR
  57. MaxSdkCallbacks.EmitSdkInitializedEvent();
  58. #endif
  59. });
  60. }
  61. /// <summary>
  62. /// Check if the SDK has been initialized.
  63. /// </summary>
  64. /// <returns>True if SDK has been initialized</returns>
  65. public static bool IsInitialized()
  66. {
  67. return _isInitialized;
  68. }
  69. /// <summary>
  70. /// Prevent stub ads from showing in the Unity Editor
  71. /// </summary>
  72. public static void DisableStubAds()
  73. {
  74. _showStubAds = false;
  75. }
  76. #endregion
  77. #region User Info
  78. /// <summary>
  79. /// Set an identifier for the current user. This identifier will be tied to SDK events and our optional S2S postbacks.
  80. ///
  81. /// If you're using reward validation, you can optionally set an identifier to be included with currency validation postbacks.
  82. /// For example, a username or email. We'll include this in the postback when we ping your currency endpoint from our server.
  83. /// </summary>
  84. ///
  85. /// <param name="userId">The user identifier to be set. Must not be null.</param>
  86. public static void SetUserId(string userId) { }
  87. /// <summary>
  88. /// Set the <see cref="MaxSegmentCollection"/>.
  89. /// </summary>
  90. /// <param name="segmentCollection"> The segment collection to be set. Must not be {@code null}</param>
  91. public static void SetSegmentCollection(MaxSegmentCollection segmentCollection)
  92. {
  93. if (_isInitialized)
  94. {
  95. MaxSdkLogger.UserError("Segment collection must be set before MAX SDK is initialized ");
  96. }
  97. }
  98. #endregion
  99. #region MAX
  100. /// <summary>
  101. /// Returns the list of available mediation networks.
  102. ///
  103. /// Please call this method after the SDK has initialized.
  104. /// </summary>
  105. public static List<MaxSdkBase.MediatedNetworkInfo> GetAvailableMediatedNetworks()
  106. {
  107. return new List<MaxSdkBase.MediatedNetworkInfo>();
  108. }
  109. /// <summary>
  110. /// Present the mediation debugger UI.
  111. /// This debugger tool provides the status of your integration for each third-party ad network.
  112. ///
  113. /// Please call this method after the SDK has initialized.
  114. /// </summary>
  115. public static void ShowMediationDebugger()
  116. {
  117. if (!_isInitialized)
  118. {
  119. MaxSdkLogger.UserWarning("The mediation debugger cannot be shown before the MAX SDK has been initialized."
  120. + "\nCall 'MaxSdk.InitializeSdk();' and listen for 'MaxSdkCallbacks.OnSdkInitializedEvent' before showing the mediation debugger.");
  121. }
  122. else
  123. {
  124. MaxSdkLogger.UserWarning("The mediation debugger cannot be shown in the Unity Editor. Please export the project to Android or iOS first.");
  125. }
  126. }
  127. /// <summary>
  128. /// Present the mediation debugger UI.
  129. ///
  130. /// Please call this method after the SDK has initialized.
  131. /// </summary>
  132. public static void ShowCreativeDebugger()
  133. {
  134. if (!_isInitialized)
  135. {
  136. MaxSdkLogger.UserWarning("The creative debugger cannot be shown before the MAX SDK has been initialized."
  137. + "\nCall 'MaxSdk.InitializeSdk();' and listen for 'MaxSdkCallbacks.OnSdkInitializedEvent' before showing the mediation debugger.");
  138. }
  139. else
  140. {
  141. MaxSdkLogger.UserWarning("The creative debugger cannot be shown in the Unity Editor. Please export the project to Android or iOS first.");
  142. }
  143. }
  144. /// <summary>
  145. /// Returns the arbitrary ad value for a given ad unit identifier with key. Returns null if no ad is loaded.
  146. /// </summary>
  147. /// <param name="adUnitIdentifier">Ad unit identifier for which to get the ad value for. Must not be null.</param>
  148. /// <param name="key">Ad value key. Must not be null.</param>
  149. /// <returns>Arbitrary ad value for a given key, or null if no ad is loaded.</returns>
  150. public static string GetAdValue(string adUnitIdentifier, string key)
  151. {
  152. return "";
  153. }
  154. #endregion
  155. #region Privacy
  156. /// <summary>
  157. /// Get the SDK configuration for this user.
  158. ///
  159. /// Note: This method should be called only after SDK has been initialized.
  160. /// </summary>
  161. public static SdkConfiguration GetSdkConfiguration()
  162. {
  163. return SdkConfiguration.CreateEmpty();
  164. }
  165. /// <summary>
  166. /// Set whether or not user has provided consent for information sharing with AppLovin and other providers.
  167. /// </summary>
  168. /// <param name="hasUserConsent"><c>true</c> if the user has provided consent for information sharing with AppLovin. <c>false</c> by default.</param>
  169. public static void SetHasUserConsent(bool hasUserConsent)
  170. {
  171. _hasUserConsent = hasUserConsent;
  172. _isUserConsentSet = true;
  173. }
  174. /// <summary>
  175. /// Check if user has provided consent for information sharing with AppLovin and other providers.
  176. /// </summary>
  177. /// <returns><c>true</c> if user has provided consent for information sharing. <c>false</c> if the user declined to share information or the consent value has not been set <see cref="IsUserConsentSet"/>.</returns>
  178. public static bool HasUserConsent()
  179. {
  180. return _hasUserConsent;
  181. }
  182. /// <summary>
  183. /// Check if user has set consent for information sharing.
  184. /// </summary>
  185. /// <returns><c>true</c> if user has set a value of consent for information sharing.</returns>
  186. public static bool IsUserConsentSet()
  187. {
  188. return _isUserConsentSet;
  189. }
  190. /// <summary>
  191. /// Set whether or not user has opted out of the sale of their personal information.
  192. /// </summary>
  193. /// <param name="doNotSell"><c>true</c> if the user has opted out of the sale of their personal information.</param>
  194. public static void SetDoNotSell(bool doNotSell)
  195. {
  196. _doNotSell = doNotSell;
  197. _isDoNotSellSet = true;
  198. }
  199. /// <summary>
  200. /// Check if the user has opted out of the sale of their personal information.
  201. /// </summary>
  202. /// <returns><c>true</c> if the user has opted out of the sale of their personal information. <c>false</c> if the user opted in to the sell of their personal information or the value has not been set <see cref="IsDoNotSellSet"/>.</returns>
  203. public static bool IsDoNotSell()
  204. {
  205. return _doNotSell;
  206. }
  207. /// <summary>
  208. /// Check if the user has set the option to sell their personal information.
  209. /// </summary>
  210. /// <returns><c>true</c> if user has chosen an option to sell their personal information.</returns>
  211. public static bool IsDoNotSellSet()
  212. {
  213. return _isDoNotSellSet;
  214. }
  215. #endregion
  216. #region Banners
  217. /// <summary>
  218. /// Create a new banner.
  219. /// </summary>
  220. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to create. Must not be null.</param>
  221. /// <param name="configuration">The configuration for the banner</param>
  222. public static void CreateBanner(string adUnitIdentifier, AdViewConfiguration configuration)
  223. {
  224. ValidateAdUnitIdentifier(adUnitIdentifier, "create banner");
  225. RequestAdUnit(adUnitIdentifier);
  226. if (configuration.UseCoordinates) return;
  227. if (_showStubAds && !StubBanners.ContainsKey(adUnitIdentifier))
  228. {
  229. CreateStubBanner(adUnitIdentifier, configuration.Position);
  230. }
  231. ExecuteWithDelay(1f, () =>
  232. {
  233. var placement = MaxSdkUtils.GetStringFromDictionary(BannerPlacements, adUnitIdentifier);
  234. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnBannerAdLoadedEvent", adUnitIdentifier, placement));
  235. MaxSdkCallbacks.ForwardEvent(eventProps);
  236. });
  237. }
  238. private static void CreateStubBanner(string adUnitIdentifier, AdViewPosition bannerPosition)
  239. {
  240. #if UNITY_EDITOR
  241. // Only support BottomCenter and TopCenter for now
  242. var bannerPrefabName = bannerPosition == AdViewPosition.BottomCenter ? "BannerBottom" : "BannerTop";
  243. var prefabPath = MaxSdkUtils.GetAssetPathForExportPath("MaxSdk/Prefabs/" + bannerPrefabName + ".prefab");
  244. var bannerPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  245. var stubBanner = Object.Instantiate(bannerPrefab, Vector3.zero, Quaternion.identity);
  246. stubBanner.SetActive(false); // Hidden by default
  247. Object.DontDestroyOnLoad(stubBanner);
  248. var bannerText = stubBanner.GetComponentInChildren<Text>();
  249. bannerText.text += ":\n" + adUnitIdentifier;
  250. StubBanners.Add(adUnitIdentifier, stubBanner);
  251. #endif
  252. }
  253. /// <summary>
  254. /// Load a new banner ad.
  255. /// NOTE: The <see cref="CreateBanner()"/> method loads the first banner ad and initiates an automated banner refresh process.
  256. /// You only need to call this method if you pause banner refresh.
  257. /// </summary>
  258. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to load. Must not be null.</param>
  259. public static void LoadBanner(string adUnitIdentifier)
  260. {
  261. ValidateAdUnitIdentifier(adUnitIdentifier, "load banner");
  262. ExecuteWithDelay(1f, () =>
  263. {
  264. var placement = MaxSdkUtils.GetStringFromDictionary(BannerPlacements, adUnitIdentifier);
  265. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnBannerAdLoadedEvent", adUnitIdentifier, placement));
  266. MaxSdkCallbacks.ForwardEvent(eventProps);
  267. });
  268. }
  269. /// <summary>
  270. /// Set the banner placement for an ad unit identifier to tie the future ad events to.
  271. /// </summary>
  272. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the placement for. Must not be null.</param>
  273. /// <param name="placement">Placement to set</param>
  274. public static void SetBannerPlacement(string adUnitIdentifier, string placement)
  275. {
  276. BannerPlacements[adUnitIdentifier] = placement;
  277. MaxSdkLogger.UserDebug("Setting banner placement to '" + placement + "' for ad unit id '" + adUnitIdentifier + "'");
  278. }
  279. /// <summary>
  280. /// Starts or resumes auto-refreshing of the banner for the given ad unit identifier.
  281. /// </summary>
  282. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to start auto-refresh. Must not be null.</param>
  283. public static void StartBannerAutoRefresh(string adUnitIdentifier)
  284. {
  285. MaxSdkLogger.UserDebug("Starting banner auto refresh.");
  286. }
  287. /// <summary>
  288. /// Pauses auto-refreshing of the banner for the given ad unit identifier.
  289. /// </summary>
  290. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to stop auto-refresh. Must not be null.</param>
  291. public static void StopBannerAutoRefresh(string adUnitIdentifier)
  292. {
  293. MaxSdkLogger.UserDebug("Stopping banner auto refresh.");
  294. }
  295. /// <summary>
  296. /// Updates the position of the banner to the new position provided.
  297. /// </summary>
  298. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to update the position. Must not be null.</param>
  299. /// <param name="bannerPosition">A new position for the banner. Must not be null.</param>
  300. public static void UpdateBannerPosition(string adUnitIdentifier, AdViewPosition bannerPosition)
  301. {
  302. MaxSdkLogger.D("[AppLovin MAX] Updating banner position to '" + bannerPosition + "' for ad unit id '" + adUnitIdentifier + "'");
  303. }
  304. /// <summary>
  305. /// Updates the position of the banner to the new coordinates provided.
  306. /// </summary>
  307. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to update the position. Must not be null.</param>
  308. /// <param name="x">The X coordinate (horizontal position) of the banner relative to the top left corner of the screen.</param>
  309. /// <param name="y">The Y coordinate (vertical position) of the banner relative to the top left corner of the screen.</param>
  310. /// <seealso cref="GetBannerLayout">
  311. /// The banner is placed within the safe area of the screen. You can use this to get the absolute position of the banner on screen.
  312. /// </seealso>
  313. public static void UpdateBannerPosition(string adUnitIdentifier, float x, float y)
  314. {
  315. MaxSdkLogger.UserDebug("Updating banner position to '(" + x + "," + y + ")");
  316. }
  317. /// <summary>
  318. /// Overrides the width of the banner in points/dp.
  319. /// </summary>
  320. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to override the width for. Must not be null.</param>
  321. /// <param name="width">The desired width of the banner in points/dp</param>
  322. public static void SetBannerWidth(string adUnitIdentifier, float width)
  323. {
  324. // NOTE: Will implement in a future release
  325. MaxSdkLogger.D("[AppLovin MAX] Set banner width to '" + width + "' for ad unit id '" + adUnitIdentifier + "'");
  326. }
  327. /// <summary>
  328. /// Show banner at a position determined by the 'CreateBanner' call.
  329. /// </summary>
  330. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to show. Must not be null.</param>
  331. public static void ShowBanner(string adUnitIdentifier)
  332. {
  333. ValidateAdUnitIdentifier(adUnitIdentifier, "show banner");
  334. if (!IsAdUnitRequested(adUnitIdentifier))
  335. {
  336. MaxSdkLogger.UserWarning("Banner '" + adUnitIdentifier + "' was not created, can not show it");
  337. }
  338. else
  339. {
  340. GameObject stubBanner;
  341. if (StubBanners.TryGetValue(adUnitIdentifier, out stubBanner))
  342. {
  343. stubBanner.SetActive(true);
  344. }
  345. }
  346. }
  347. /// <summary>
  348. /// Remove banner from the ad view and destroy it.
  349. /// </summary>
  350. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to destroy. Must not be null.</param>
  351. public static void DestroyBanner(string adUnitIdentifier)
  352. {
  353. ValidateAdUnitIdentifier(adUnitIdentifier, "destroy banner");
  354. GameObject stubBanner;
  355. if (StubBanners.TryGetValue(adUnitIdentifier, out stubBanner))
  356. {
  357. Object.Destroy(stubBanner);
  358. StubBanners.Remove(adUnitIdentifier);
  359. }
  360. }
  361. /// <summary>
  362. /// Hide banner.
  363. /// </summary>
  364. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to hide. Must not be null.</param>
  365. public static void HideBanner(string adUnitIdentifier)
  366. {
  367. ValidateAdUnitIdentifier(adUnitIdentifier, "hide banner");
  368. GameObject stubBanner;
  369. if (StubBanners.TryGetValue(adUnitIdentifier, out stubBanner))
  370. {
  371. stubBanner.SetActive(false);
  372. }
  373. }
  374. /// <summary>
  375. /// Set non-transparent background color for banners to be fully functional.
  376. /// </summary>
  377. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set background color for. Must not be null.</param>
  378. /// <param name="color">A background color to set for the ad. Must not be null.</param>
  379. public static void SetBannerBackgroundColor(string adUnitIdentifier, Color color)
  380. {
  381. ValidateAdUnitIdentifier(adUnitIdentifier, "set background color");
  382. GameObject stubBanner;
  383. if (StubBanners.TryGetValue(adUnitIdentifier, out stubBanner))
  384. {
  385. stubBanner.GetComponentInChildren<Image>().color = color;
  386. }
  387. }
  388. /// <summary>
  389. /// Set an extra parameter for the banner ad.
  390. /// </summary>
  391. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the extra parameter for. Must not be null.</param>
  392. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  393. /// <param name="value">The value for the extra parameter.</param>
  394. public static void SetBannerExtraParameter(string adUnitIdentifier, string key, string value)
  395. {
  396. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner extra parameter");
  397. }
  398. /// <summary>
  399. /// Set a local extra parameter for the banner ad.
  400. /// </summary>
  401. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the local extra parameter for. Must not be null.</param>
  402. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  403. /// <param name="value">The value for the local extra parameter.</param>
  404. public static void SetBannerLocalExtraParameter(string adUnitIdentifier, string key, object value)
  405. {
  406. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner local extra parameter");
  407. }
  408. /// <summary>
  409. /// The custom data to tie the showing banner ad to, for ILRD and rewarded postbacks via the <c>{CUSTOM_DATA}</c> macro. Maximum size is 8KB.
  410. /// </summary>
  411. /// <param name="adUnitIdentifier">Banner ad unit identifier of the banner to set the custom data for. Must not be null.</param>
  412. /// <param name="customData">The custom data to be set.</param>
  413. public static void SetBannerCustomData(string adUnitIdentifier, string customData)
  414. {
  415. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner custom data");
  416. }
  417. /// <summary>
  418. /// The banner position on the screen. When setting the banner position via <see cref="CreateBanner(string, float, float)"/> or <see cref="UpdateBannerPosition(string, float, float)"/>,
  419. /// the banner is placed within the safe area of the screen. This returns the absolute position of the banner on screen.
  420. /// </summary>
  421. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to get the position on screen. Must not be null.</param>
  422. /// <returns>A <see cref="Rect"/> representing the banner position on screen.</returns>
  423. public static Rect GetBannerLayout(string adUnitIdentifier)
  424. {
  425. ValidateAdUnitIdentifier(adUnitIdentifier, "get banner layout");
  426. return Rect.zero;
  427. }
  428. #endregion
  429. #region MRECs
  430. /// <summary>
  431. /// Create a new MREC.
  432. /// </summary>
  433. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to create. Must not be null.</param>
  434. /// <param name="configuration">The configuration for the MREC.</param>
  435. public static void CreateMRec(string adUnitIdentifier, AdViewConfiguration configuration)
  436. {
  437. ValidateAdUnitIdentifier(adUnitIdentifier, "create MREC");
  438. RequestAdUnit(adUnitIdentifier);
  439. if (configuration.UseCoordinates) return;
  440. ExecuteWithDelay(1f, () =>
  441. {
  442. var placement = MaxSdkUtils.GetStringFromDictionary(MRecPlacements, adUnitIdentifier);
  443. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnMRecAdLoadedEvent", adUnitIdentifier, placement));
  444. MaxSdkCallbacks.ForwardEvent(eventProps);
  445. });
  446. }
  447. /// <summary>
  448. /// Load a new MREC ad.
  449. /// NOTE: The <see cref="CreateMRec()"/> method loads the first MREC ad and initiates an automated MREC refresh process.
  450. /// You only need to call this method if you pause MREC refresh.
  451. /// </summary>
  452. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to load. Must not be null.</param>
  453. public static void LoadMRec(string adUnitIdentifier)
  454. {
  455. ValidateAdUnitIdentifier(adUnitIdentifier, "load MREC");
  456. ExecuteWithDelay(1f, () =>
  457. {
  458. var placement = MaxSdkUtils.GetStringFromDictionary(MRecPlacements, adUnitIdentifier);
  459. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnMRecAdLoadedEvent", adUnitIdentifier, placement));
  460. MaxSdkCallbacks.ForwardEvent(eventProps);
  461. });
  462. }
  463. /// <summary>
  464. /// Set the MREC placement for an ad unit identifier to tie the future ad events to.
  465. /// </summary>
  466. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the placement for. Must not be null.</param>
  467. /// <param name="placement">Placement to set</param>
  468. public static void SetMRecPlacement(string adUnitIdentifier, string placement)
  469. {
  470. MRecPlacements[adUnitIdentifier] = placement;
  471. MaxSdkLogger.UserDebug("Setting MREC placement to '" + placement + "' for ad unit id '" + adUnitIdentifier + "'");
  472. }
  473. /// <summary>
  474. /// Starts or resumes auto-refreshing of the MREC for the given ad unit identifier.
  475. /// </summary>
  476. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to start auto-refresh. Must not be null.</param>
  477. public static void StartMRecAutoRefresh(string adUnitIdentifier)
  478. {
  479. MaxSdkLogger.UserDebug("Starting banner auto refresh.");
  480. }
  481. /// <summary>
  482. /// Pauses auto-refreshing of the MREC for the given ad unit identifier.
  483. /// </summary>
  484. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to stop auto-refresh. Must not be null.</param>
  485. public static void StopMRecAutoRefresh(string adUnitIdentifier)
  486. {
  487. MaxSdkLogger.UserDebug("Stopping banner auto refresh.");
  488. }
  489. /// <summary>
  490. /// Updates the position of the MREC to the new position provided.
  491. /// </summary>
  492. /// <param name="adUnitIdentifier">The ad unit identifier of the MREC for which to update the position. Must not be null.</param>
  493. /// <param name="mrecPosition">A new position for the MREC. Must not be null.</param>
  494. public static void UpdateMRecPosition(string adUnitIdentifier, AdViewPosition mrecPosition)
  495. {
  496. MaxSdkLogger.UserDebug("Updating MREC position to '" + mrecPosition + "' for ad unit id '" + adUnitIdentifier + "'");
  497. }
  498. /// <summary>
  499. /// Updates the position of the MREC to the new coordinates provided.
  500. /// </summary>
  501. /// <param name="adUnitIdentifier">The ad unit identifier of the MREC for which to update the position. Must not be null.</param>
  502. /// <param name="x">The X coordinate (horizontal position) of the MREC relative to the top left corner of the screen.</param>
  503. /// <param name="y">The Y coordinate (vertical position) of the MREC relative to the top left corner of the screen.</param>
  504. /// <seealso cref="GetMRecLayout">
  505. /// The MREC is placed within the safe area of the screen. You can use this to get the absolute position Rect of the MREC on screen.
  506. /// </seealso>
  507. public static void UpdateMRecPosition(string adUnitIdentifier, float x, float y)
  508. {
  509. MaxSdkLogger.UserDebug("Updating MREC position to '(" + x + "," + y + ")");
  510. }
  511. /// <summary>
  512. /// Show MREC at a position determined by the 'CreateMRec' call.
  513. /// </summary>
  514. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to show. Must not be null.</param>
  515. public static void ShowMRec(string adUnitIdentifier)
  516. {
  517. ValidateAdUnitIdentifier(adUnitIdentifier, "show MREC");
  518. if (!IsAdUnitRequested(adUnitIdentifier))
  519. {
  520. MaxSdkLogger.UserWarning("MREC '" + adUnitIdentifier + "' was not created, can not show it");
  521. }
  522. }
  523. /// <summary>
  524. /// Remove MREC from the ad view and destroy it.
  525. /// </summary>
  526. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to destroy. Must not be null.</param>
  527. public static void DestroyMRec(string adUnitIdentifier)
  528. {
  529. ValidateAdUnitIdentifier(adUnitIdentifier, "destroy MREC");
  530. }
  531. /// <summary>
  532. /// Hide MREC.
  533. /// </summary>
  534. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to hide. Must not be null.</param>
  535. public static void HideMRec(string adUnitIdentifier)
  536. {
  537. ValidateAdUnitIdentifier(adUnitIdentifier, "hide MREC");
  538. }
  539. /// <summary>
  540. /// Set an extra parameter for the MREC ad.
  541. /// </summary>
  542. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the extra parameter for. Must not be null.</param>
  543. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  544. /// <param name="value">The value for the extra parameter.</param>
  545. public static void SetMRecExtraParameter(string adUnitIdentifier, string key, string value)
  546. {
  547. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC extra parameter");
  548. }
  549. /// <summary>
  550. /// Set a local extra parameter for the MREC ad.
  551. /// </summary>
  552. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the local extra parameter for. Must not be null.</param>
  553. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  554. /// <param name="value">The value for the local extra parameter.</param>
  555. public static void SetMRecLocalExtraParameter(string adUnitIdentifier, string key, object value)
  556. {
  557. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC local extra parameter");
  558. }
  559. /// <summary>
  560. /// The custom data to tie the showing MREC ad to, for ILRD and rewarded postbacks via the <c>{CUSTOM_DATA}</c> macro. Maximum size is 8KB.
  561. /// </summary>
  562. /// <param name="adUnitIdentifier">MREC Ad unit identifier of the banner to set the custom data for. Must not be null.</param>
  563. /// <param name="customData">The custom data to be set.</param>
  564. public static void SetMRecCustomData(string adUnitIdentifier, string customData)
  565. {
  566. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC custom data");
  567. }
  568. /// <summary>
  569. /// The MREC position on the screen. When setting the MREC position via <see cref="CreateMRec(string, float, float)"/> or <see cref="UpdateMRecPosition(string, float, float)"/>,
  570. /// the MREC is placed within the safe area of the screen. This returns the absolute position of the MREC on screen.
  571. /// </summary>
  572. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to get the position on screen. Must not be null.</param>
  573. /// <returns>A <see cref="Rect"/> representing the banner position on screen.</returns>
  574. public static Rect GetMRecLayout(string adUnitIdentifier)
  575. {
  576. ValidateAdUnitIdentifier(adUnitIdentifier, "get MREC layout");
  577. return Rect.zero;
  578. }
  579. #endregion
  580. #region Interstitials
  581. /// <summary>
  582. /// Start loading an interstitial.
  583. /// </summary>
  584. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load</param>
  585. public static void LoadInterstitial(string adUnitIdentifier)
  586. {
  587. ValidateAdUnitIdentifier(adUnitIdentifier, "load interstitial");
  588. RequestAdUnit(adUnitIdentifier);
  589. ExecuteWithDelay(1f, () =>
  590. {
  591. AddReadyAdUnit(adUnitIdentifier);
  592. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnInterstitialLoadedEvent", adUnitIdentifier));
  593. MaxSdkCallbacks.ForwardEvent(eventProps);
  594. });
  595. }
  596. /// <summary>
  597. /// Check if interstitial ad is loaded and ready to be displayed.
  598. /// </summary>
  599. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load</param>
  600. /// <returns>True if the ad is ready to be displayed</returns>
  601. public static bool IsInterstitialReady(string adUnitIdentifier)
  602. {
  603. ValidateAdUnitIdentifier(adUnitIdentifier, "check interstitial loaded");
  604. if (!IsAdUnitRequested(adUnitIdentifier))
  605. {
  606. MaxSdkLogger.UserWarning("Interstitial '" + adUnitIdentifier +
  607. "' was not requested, can not check if it is loaded");
  608. return false;
  609. }
  610. return IsAdUnitReady(adUnitIdentifier);
  611. }
  612. /// <summary>
  613. /// Present loaded interstitial for a given placement to tie ad events to. Note: if the interstitial is not ready to be displayed nothing will happen.
  614. /// </summary>
  615. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load</param>
  616. /// <param name="placement">The placement to tie the showing ad's events to</param>
  617. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  618. public static void ShowInterstitial(string adUnitIdentifier, string placement = null, string customData = null)
  619. {
  620. ValidateAdUnitIdentifier(adUnitIdentifier, "show interstitial");
  621. if (!IsAdUnitRequested(adUnitIdentifier))
  622. {
  623. MaxSdkLogger.UserWarning(
  624. "Interstitial '" + adUnitIdentifier + "' was not requested, can not show it");
  625. return;
  626. }
  627. if (!IsInterstitialReady(adUnitIdentifier))
  628. {
  629. MaxSdkLogger.UserWarning("Interstitial '" + adUnitIdentifier + "' is not ready, please check IsInterstitialReady() before showing.");
  630. return;
  631. }
  632. RemoveReadyAdUnit(adUnitIdentifier);
  633. if (_showStubAds)
  634. {
  635. ShowStubInterstitial(adUnitIdentifier, placement);
  636. }
  637. }
  638. private static void ShowStubInterstitial(string adUnitIdentifier, string placement)
  639. {
  640. #if UNITY_EDITOR
  641. var prefabPath = MaxSdkUtils.GetAssetPathForExportPath("MaxSdk/Prefabs/Interstitial.prefab");
  642. var interstitialPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  643. var stubInterstitial = Object.Instantiate(interstitialPrefab, Vector3.zero, Quaternion.identity);
  644. var interstitialText = GameObject.Find("MaxInterstitialTitle").GetComponent<Text>();
  645. var closeButton = GameObject.Find("MaxInterstitialCloseButton").GetComponent<Button>();
  646. Object.DontDestroyOnLoad(stubInterstitial);
  647. interstitialText.text += ":\n" + adUnitIdentifier;
  648. closeButton.onClick.AddListener(() =>
  649. {
  650. var adHiddenEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnInterstitialHiddenEvent", adUnitIdentifier, placement));
  651. MaxSdkCallbacks.ForwardEvent(adHiddenEventProps);
  652. Object.Destroy(stubInterstitial);
  653. });
  654. var adDisplayedEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnInterstitialDisplayedEvent", adUnitIdentifier, placement));
  655. MaxSdkCallbacks.ForwardEvent(adDisplayedEventProps);
  656. #endif
  657. }
  658. /// <summary>
  659. /// Set an extra parameter for the ad.
  660. /// </summary>
  661. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to set the extra parameter for.</param>
  662. /// <param name="key">The key for the extra parameter.</param>
  663. /// <param name="value">The value for the extra parameter.</param>
  664. public static void SetInterstitialExtraParameter(string adUnitIdentifier, string key, string value)
  665. {
  666. ValidateAdUnitIdentifier(adUnitIdentifier, "set interstitial extra parameter");
  667. }
  668. /// <summary>
  669. /// Set a local extra parameter for the ad.
  670. /// </summary>
  671. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to set the local extra parameter for.</param>
  672. /// <param name="key">The key for the local extra parameter.</param>
  673. /// <param name="value">The value for the local extra parameter.</param>
  674. public static void SetInterstitialLocalExtraParameter(string adUnitIdentifier, string key, object value)
  675. {
  676. ValidateAdUnitIdentifier(adUnitIdentifier, "set interstitial local extra parameter");
  677. }
  678. #endregion
  679. #region App Open Ads
  680. /// <summary>
  681. /// Start loading an app open ad.
  682. /// </summary>
  683. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  684. public static void LoadAppOpenAd(string adUnitIdentifier)
  685. {
  686. ValidateAdUnitIdentifier(adUnitIdentifier, "load app open ad");
  687. RequestAdUnit(adUnitIdentifier);
  688. ExecuteWithDelay(1f, () =>
  689. {
  690. AddReadyAdUnit(adUnitIdentifier);
  691. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnAppOpenAdLoadedEvent", adUnitIdentifier));
  692. MaxSdkCallbacks.ForwardEvent(eventProps);
  693. });
  694. }
  695. /// <summary>
  696. /// Check if app open ad ad is loaded and ready to be displayed.
  697. /// </summary>
  698. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  699. /// <returns>True if the ad is ready to be displayed</returns>
  700. public static bool IsAppOpenAdReady(string adUnitIdentifier)
  701. {
  702. ValidateAdUnitIdentifier(adUnitIdentifier, "check app open ad loaded");
  703. if (!IsAdUnitRequested(adUnitIdentifier))
  704. {
  705. MaxSdkLogger.UserWarning("App Open Ad '" + adUnitIdentifier +
  706. "' was not requested, can not check if it is loaded");
  707. return false;
  708. }
  709. return IsAdUnitReady(adUnitIdentifier);
  710. }
  711. /// <summary>
  712. /// Present loaded app open ad for a given placement to tie ad events to. Note: if the app open ad is not ready to be displayed nothing will happen.
  713. /// </summary>
  714. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  715. /// <param name="placement">The placement to tie the showing ad's events to</param>
  716. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  717. public static void ShowAppOpenAd(string adUnitIdentifier, string placement = null, string customData = null)
  718. {
  719. ValidateAdUnitIdentifier(adUnitIdentifier, "show app open ad");
  720. if (!IsAdUnitRequested(adUnitIdentifier))
  721. {
  722. MaxSdkLogger.UserWarning(
  723. "App Open Ad '" + adUnitIdentifier + "' was not requested, can not show it");
  724. return;
  725. }
  726. if (!IsAppOpenAdReady(adUnitIdentifier))
  727. {
  728. MaxSdkLogger.UserWarning("App Open Ad '" + adUnitIdentifier + "' is not ready, please check IsAppOpenAdReady() before showing.");
  729. return;
  730. }
  731. RemoveReadyAdUnit(adUnitIdentifier);
  732. if (_showStubAds)
  733. {
  734. ShowStubAppOpenAd(adUnitIdentifier, placement);
  735. }
  736. }
  737. private static void ShowStubAppOpenAd(string adUnitIdentifier, string placement)
  738. {
  739. #if UNITY_EDITOR
  740. var prefabPath = MaxSdkUtils.GetAssetPathForExportPath("MaxSdk/Prefabs/Interstitial.prefab");
  741. var appOpenAdPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  742. var stubAppOpenAd = Object.Instantiate(appOpenAdPrefab, Vector3.zero, Quaternion.identity);
  743. var appOpenAdText = GameObject.Find("MaxInterstitialTitle").GetComponent<Text>();
  744. var closeButton = GameObject.Find("MaxInterstitialCloseButton").GetComponent<Button>();
  745. Object.DontDestroyOnLoad(stubAppOpenAd);
  746. appOpenAdText.text = "MAX App Open Ad:\n" + adUnitIdentifier;
  747. closeButton.onClick.AddListener(() =>
  748. {
  749. var adHiddenEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnAppOpenAdHiddenEvent", adUnitIdentifier, placement));
  750. MaxSdkCallbacks.ForwardEvent(adHiddenEventProps);
  751. Object.Destroy(stubAppOpenAd);
  752. });
  753. var adDisplayedEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnAppOpenAdDisplayedEvent", adUnitIdentifier, placement));
  754. MaxSdkCallbacks.ForwardEvent(adDisplayedEventProps);
  755. #endif
  756. }
  757. /// <summary>
  758. /// Set an extra parameter for the ad.
  759. /// </summary>
  760. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to set the extra parameter for. Must not be null.</param>
  761. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  762. /// <param name="value">The value for the extra parameter.</param>
  763. public static void SetAppOpenAdExtraParameter(string adUnitIdentifier, string key, string value)
  764. {
  765. ValidateAdUnitIdentifier(adUnitIdentifier, "set app open ad extra parameter");
  766. }
  767. /// <summary>
  768. /// Set a local extra parameter for the ad.
  769. /// </summary>
  770. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to set the local extra parameter for. Must not be null.</param>
  771. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  772. /// <param name="value">The value for the local extra parameter.</param>
  773. public static void SetAppOpenAdLocalExtraParameter(string adUnitIdentifier, string key, object value)
  774. {
  775. ValidateAdUnitIdentifier(adUnitIdentifier, "set app open ad local extra parameter");
  776. }
  777. #endregion
  778. #region Rewarded
  779. /// <summary>
  780. /// Start loading an rewarded ad.
  781. /// </summary>
  782. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to load. Must not be null.</param>
  783. public static void LoadRewardedAd(string adUnitIdentifier)
  784. {
  785. ValidateAdUnitIdentifier(adUnitIdentifier, "load rewarded ad");
  786. RequestAdUnit(adUnitIdentifier);
  787. ExecuteWithDelay(1f, () =>
  788. {
  789. AddReadyAdUnit(adUnitIdentifier);
  790. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnRewardedAdLoadedEvent", adUnitIdentifier));
  791. MaxSdkCallbacks.ForwardEvent(eventProps);
  792. });
  793. }
  794. /// <summary>
  795. /// Check if rewarded ad ad is loaded and ready to be displayed.
  796. /// </summary>
  797. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to load. Must not be null.</param>
  798. /// <returns>True if the ad is ready to be displayed</returns>
  799. public static bool IsRewardedAdReady(string adUnitIdentifier)
  800. {
  801. ValidateAdUnitIdentifier(adUnitIdentifier, "check rewarded ad loaded");
  802. if (!IsAdUnitRequested(adUnitIdentifier))
  803. {
  804. MaxSdkLogger.UserWarning("Rewarded ad '" + adUnitIdentifier +
  805. "' was not requested, can not check if it is loaded");
  806. return false;
  807. }
  808. return IsAdUnitReady(adUnitIdentifier);
  809. }
  810. /// <summary>
  811. /// Present loaded rewarded ad for a given placement to tie ad events to. Note: if the rewarded ad is not ready to be displayed nothing will happen.
  812. /// </summary>
  813. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load. Must not be null.</param>
  814. /// <param name="placement">The placement to tie the showing ad's events to</param>
  815. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  816. public static void ShowRewardedAd(string adUnitIdentifier, string placement = null, string customData = null)
  817. {
  818. ValidateAdUnitIdentifier(adUnitIdentifier, "show rewarded ad");
  819. if (!IsAdUnitRequested(adUnitIdentifier))
  820. {
  821. MaxSdkLogger.UserWarning("Rewarded ad '" + adUnitIdentifier +
  822. "' was not requested, can not show it");
  823. return;
  824. }
  825. if (!IsRewardedAdReady(adUnitIdentifier))
  826. {
  827. MaxSdkLogger.UserWarning("Rewarded ad '" + adUnitIdentifier + "' is not ready, please check IsRewardedAdReady() before showing.");
  828. return;
  829. }
  830. RemoveReadyAdUnit(adUnitIdentifier);
  831. if (_showStubAds)
  832. {
  833. ShowStubRewardedAd(adUnitIdentifier, placement);
  834. }
  835. }
  836. private static void ShowStubRewardedAd(string adUnitIdentifier, string placement)
  837. {
  838. #if UNITY_EDITOR
  839. var prefabPath = MaxSdkUtils.GetAssetPathForExportPath("MaxSdk/Prefabs/Rewarded.prefab");
  840. var rewardedPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  841. var stubRewardedAd = Object.Instantiate(rewardedPrefab, Vector3.zero, Quaternion.identity);
  842. var grantedReward = false;
  843. var rewardedTitle = GameObject.Find("MaxRewardTitle").GetComponent<Text>();
  844. var rewardStatus = GameObject.Find("MaxRewardStatus").GetComponent<Text>();
  845. var closeButton = GameObject.Find("MaxRewardedCloseButton").GetComponent<Button>();
  846. var rewardButton = GameObject.Find("MaxRewardButton").GetComponent<Button>();
  847. Object.DontDestroyOnLoad(stubRewardedAd);
  848. rewardedTitle.text += ":\n" + adUnitIdentifier;
  849. closeButton.onClick.AddListener(() =>
  850. {
  851. if (grantedReward)
  852. {
  853. var rewardEventPropsDict = CreateBaseEventPropsDictionary("OnRewardedAdReceivedRewardEvent", adUnitIdentifier, placement);
  854. rewardEventPropsDict["rewardLabel"] = "coins";
  855. rewardEventPropsDict["rewardAmount"] = "5";
  856. var rewardEventProps = Json.Serialize(rewardEventPropsDict);
  857. MaxSdkCallbacks.ForwardEvent(rewardEventProps);
  858. }
  859. var adHiddenEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnRewardedAdHiddenEvent", adUnitIdentifier, placement));
  860. MaxSdkCallbacks.ForwardEvent(adHiddenEventProps);
  861. Object.Destroy(stubRewardedAd);
  862. });
  863. rewardButton.onClick.AddListener(() =>
  864. {
  865. grantedReward = true;
  866. rewardStatus.text = "Reward granted. Will send reward callback on ad close.";
  867. });
  868. var adDisplayedEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnRewardedAdDisplayedEvent", adUnitIdentifier, placement));
  869. MaxSdkCallbacks.ForwardEvent(adDisplayedEventProps);
  870. #endif
  871. }
  872. /// <summary>
  873. /// Set an extra parameter for the ad.
  874. /// </summary>
  875. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to set the extra parameter for. Must not be null.</param>
  876. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  877. /// <param name="value">The value for the extra parameter.</param>
  878. public static void SetRewardedAdExtraParameter(string adUnitIdentifier, string key, string value)
  879. {
  880. ValidateAdUnitIdentifier(adUnitIdentifier, "set rewarded extra parameter");
  881. }
  882. /// <summary>
  883. /// Set a local extra parameter for the ad.
  884. /// </summary>
  885. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to set the local extra parameter for. Must not be null.</param>
  886. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  887. /// <param name="value">The value for the local extra parameter.</param>
  888. public static void SetRewardedAdLocalExtraParameter(string adUnitIdentifier, string key, object value)
  889. {
  890. ValidateAdUnitIdentifier(adUnitIdentifier, "set rewarded local extra parameter");
  891. }
  892. #endregion
  893. #region Event Tracking
  894. /// <summary>
  895. /// Track an event using AppLovin.
  896. /// </summary>
  897. /// <param name="name">An event from the list of pre-defined events may be found in MaxEvents.cs as part of the AppLovin SDK framework. Must not be null.</param>
  898. /// <param name="parameters">A dictionary containing key-value pairs further describing this event.</param>
  899. public static void TrackEvent(string name, IDictionary<string, string> parameters = null) { }
  900. #endregion
  901. #region Settings
  902. private static bool isMuted;
  903. /// <summary>
  904. /// Set whether to begin video ads in a muted state or not.
  905. ///
  906. /// Please call this method after the SDK has initialized.
  907. /// </summary>
  908. /// <param name="muted"><c>true</c> if video ads should being in muted state.</param>
  909. public static void SetMuted(bool muted)
  910. {
  911. isMuted = muted;
  912. }
  913. /// <summary>
  914. /// Whether video ads begin in a muted state or not. Defaults to <c>false</c>.
  915. ///
  916. /// Note: Returns <c>false</c> if the SDK is not initialized yet.
  917. /// </summary>
  918. /// <returns><c>true</c> if video ads begin in muted state.</returns>
  919. public static bool IsMuted()
  920. {
  921. return isMuted;
  922. }
  923. /// <summary>
  924. /// Toggle verbose logging of AppLovin SDK. If enabled AppLovin messages will appear in standard application log. All log messages will have "AppLovinSdk" tag.
  925. /// </summary>
  926. /// <param name="enabled"><c>true</c> if verbose logging should be enabled.</param>
  927. public static void SetVerboseLogging(bool enabled)
  928. {
  929. #if UNITY_EDITOR
  930. EditorPrefs.SetBool(MaxSdkLogger.KeyVerboseLoggingEnabled, enabled);
  931. #endif
  932. }
  933. /// <summary>
  934. /// Whether or not verbose logging is enabled.
  935. /// </summary>
  936. /// <returns><c>true</c> if verbose logging is enabled.</returns>
  937. public static bool IsVerboseLoggingEnabled()
  938. {
  939. #if UNITY_EDITOR
  940. return EditorPrefs.GetBool(MaxSdkLogger.KeyVerboseLoggingEnabled, false);
  941. #else
  942. return false;
  943. #endif
  944. }
  945. /// <summary>
  946. /// Whether the creative debugger will be displayed on fullscreen ads after flipping the device screen down twice. Defaults to true.
  947. /// </summary>
  948. /// <param name="enabled"><c>true</c> if the creative debugger should be enabled.</param>
  949. public static void SetCreativeDebuggerEnabled(bool enabled) { }
  950. /// <summary>
  951. /// Enable devices to receive test ads, by passing in the advertising identifier (IDFA/GAID) of each test device.
  952. /// Refer to AppLovin logs for the IDFA/GAID of your current device.
  953. /// </summary>
  954. /// <param name="advertisingIdentifiers">String list of advertising identifiers from devices to receive test ads.</param>
  955. public static void SetTestDeviceAdvertisingIdentifiers(string[] advertisingIdentifiers)
  956. {
  957. if (IsInitialized())
  958. {
  959. MaxSdkLogger.UserError("Test Device Advertising Identifiers must be set before SDK initialization.");
  960. }
  961. }
  962. /// <summary>
  963. /// Whether or not the native AppLovin SDKs listen to exceptions. Defaults to <c>true</c>.
  964. /// </summary>
  965. /// <param name="enabled"><c>true</c> if the native AppLovin SDKs should not listen to exceptions.</param>
  966. public static void SetExceptionHandlerEnabled(bool enabled) { }
  967. /// <summary>
  968. /// Set an extra parameter to pass to the AppLovin server.
  969. /// </summary>
  970. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  971. /// <param name="value">The value for the extra parameter. May be null.</param>
  972. public static void SetExtraParameter(string key, string value)
  973. {
  974. HandleExtraParameter(key, value);
  975. }
  976. /// <summary>
  977. /// Get the native insets in pixels for the safe area.
  978. /// These insets are used to position ads within the safe area of the screen.
  979. /// </summary>
  980. public static SafeAreaInsets GetSafeAreaInsets()
  981. {
  982. return _safeAreaInsets;
  983. }
  984. #endregion
  985. #region Internal
  986. private static void RequestAdUnit(string adUnitId)
  987. {
  988. EnsureInitialized();
  989. RequestedAdUnits.Add(adUnitId);
  990. }
  991. private static bool IsAdUnitRequested(string adUnitId)
  992. {
  993. EnsureInitialized();
  994. return RequestedAdUnits.Contains(adUnitId);
  995. }
  996. private static void AddReadyAdUnit(string adUnitId)
  997. {
  998. EnsureInitialized();
  999. ReadyAdUnits.Add(adUnitId);
  1000. }
  1001. private static bool IsAdUnitReady(string adUnitId)
  1002. {
  1003. EnsureInitialized();
  1004. return ReadyAdUnits.Contains(adUnitId);
  1005. }
  1006. private static void RemoveReadyAdUnit(string adUnitId)
  1007. {
  1008. ReadyAdUnits.Remove(adUnitId);
  1009. }
  1010. private static void EnsureInitialized()
  1011. {
  1012. if (_isInitialized) return;
  1013. MaxSdkLogger.UserWarning("MAX Ads SDK is not initialized by the time ad is requested. Please call Max.InitializeSdk() in your first scene");
  1014. }
  1015. private static Dictionary<string, string> CreateBaseEventPropsDictionary(string eventName, string adUnitId, string placement = null)
  1016. {
  1017. return new Dictionary<string, string>()
  1018. {
  1019. {"name", eventName},
  1020. {"adUnitId", adUnitId},
  1021. {"placement", placement ?? ""}
  1022. };
  1023. }
  1024. private static void ExecuteWithDelay(float seconds, Action action)
  1025. {
  1026. MaxEventExecutor.Instance.StartCoroutine(ExecuteAction(seconds, action));
  1027. }
  1028. private static IEnumerator ExecuteAction(float seconds, Action action)
  1029. {
  1030. yield return new WaitForSecondsRealtime(seconds);
  1031. action();
  1032. }
  1033. #endregion
  1034. #region Obsolete
  1035. [Obsolete("This API has been deprecated and will be removed in a future release. Please use CreateBanner(string adUnitIdentifier, AdViewConfiguration configuration) instead.")]
  1036. public static void CreateBanner(string adUnitIdentifier, BannerPosition bannerPosition)
  1037. {
  1038. // AdViewPosition and BannerPosition share identical enum values, so casting is safe
  1039. CreateBanner(adUnitIdentifier, new AdViewConfiguration((AdViewPosition) bannerPosition));
  1040. }
  1041. [Obsolete("This API has been deprecated and will be removed in a future release. Please use CreateBanner(string adUnitIdentifier, AdViewConfiguration configuration) instead.")]
  1042. public static void CreateBanner(string adUnitIdentifier, float x, float y)
  1043. {
  1044. CreateBanner(adUnitIdentifier, new AdViewConfiguration(x, y));
  1045. }
  1046. [Obsolete("This API has been deprecated and will be removed in a future release. Please use UpdateBannerPosition(string adUnitIdentifier, AdViewPosition bannerPosition) instead.")]
  1047. public static void UpdateBannerPosition(string adUnitIdentifier, BannerPosition bannerPosition)
  1048. {
  1049. // AdViewPosition and BannerPosition share identical enum values, so casting is safe
  1050. UpdateBannerPosition(adUnitIdentifier, (AdViewPosition) bannerPosition);
  1051. }
  1052. [Obsolete("This API has been deprecated and will be removed in a future release. Please use CreateMRec(string adUnitIdentifier, AdViewConfiguration configuration) instead.")]
  1053. public static void CreateMRec(string adUnitIdentifier, AdViewPosition mrecPosition)
  1054. {
  1055. CreateMRec(adUnitIdentifier, new AdViewConfiguration(mrecPosition));
  1056. }
  1057. [Obsolete("This API has been deprecated and will be removed in a future release. Please use CreateMRec(string adUnitIdentifier, AdViewConfiguration configuration) instead.")]
  1058. public static void CreateMRec(string adUnitIdentifier, float x, float y)
  1059. {
  1060. CreateMRec(adUnitIdentifier, new AdViewConfiguration(x, y));
  1061. }
  1062. [Obsolete("This API has been deprecated and will be removed in a future release. Please set your SDK key in the AppLovin Integration Manager.")]
  1063. public static void SetSdkKey(string sdkKey)
  1064. {
  1065. Debug.LogWarning("MaxSdk.SetSdkKey() has been deprecated and will be removed in a future release. Please set your SDK key in the AppLovin Integration Manager.");
  1066. }
  1067. #endregion
  1068. }
  1069. #endif