MaxSdkUnityEditor.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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="bannerPosition">Banner position. Must not be null.</param>
  222. public static void CreateBanner(string adUnitIdentifier, BannerPosition bannerPosition)
  223. {
  224. ValidateAdUnitIdentifier(adUnitIdentifier, "create banner");
  225. RequestAdUnit(adUnitIdentifier);
  226. if (_showStubAds && !StubBanners.ContainsKey(adUnitIdentifier))
  227. {
  228. CreateStubBanner(adUnitIdentifier, bannerPosition);
  229. }
  230. ExecuteWithDelay(1f, () =>
  231. {
  232. var placement = MaxSdkUtils.GetStringFromDictionary(BannerPlacements, adUnitIdentifier);
  233. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnBannerAdLoadedEvent", adUnitIdentifier, placement));
  234. MaxSdkCallbacks.ForwardEvent(eventProps);
  235. });
  236. }
  237. /// <summary>
  238. /// Create a new banner with a custom position.
  239. /// </summary>
  240. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to create. Must not be null.</param>
  241. /// <param name="x">The X coordinate (horizontal position) of the banner relative to the top left corner of the screen.</param>
  242. /// <param name="y">The Y coordinate (vertical position) of the banner relative to the top left corner of the screen.</param>
  243. /// <seealso cref="GetBannerLayout">
  244. /// 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.
  245. /// </seealso>
  246. public static void CreateBanner(string adUnitIdentifier, float x, float y)
  247. {
  248. ValidateAdUnitIdentifier(adUnitIdentifier, "create banner");
  249. RequestAdUnit(adUnitIdentifier);
  250. // TODO: Add stub ads support
  251. }
  252. private static void CreateStubBanner(string adUnitIdentifier, BannerPosition bannerPosition)
  253. {
  254. #if UNITY_EDITOR
  255. // Only support BottomCenter and TopCenter for now
  256. var bannerPrefabName = bannerPosition == BannerPosition.BottomCenter ? "BannerBottom" : "BannerTop";
  257. var prefabPath = MaxSdkUtils.GetAssetPathForExportPath("MaxSdk/Prefabs/" + bannerPrefabName + ".prefab");
  258. var bannerPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  259. var stubBanner = Object.Instantiate(bannerPrefab, Vector3.zero, Quaternion.identity);
  260. stubBanner.SetActive(false); // Hidden by default
  261. Object.DontDestroyOnLoad(stubBanner);
  262. var bannerText = stubBanner.GetComponentInChildren<Text>();
  263. bannerText.text += ":\n" + adUnitIdentifier;
  264. StubBanners.Add(adUnitIdentifier, stubBanner);
  265. #endif
  266. }
  267. /// <summary>
  268. /// Load a new banner ad.
  269. /// NOTE: The <see cref="CreateBanner()"/> method loads the first banner ad and initiates an automated banner refresh process.
  270. /// You only need to call this method if you pause banner refresh.
  271. /// </summary>
  272. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to load. Must not be null.</param>
  273. public static void LoadBanner(string adUnitIdentifier)
  274. {
  275. ValidateAdUnitIdentifier(adUnitIdentifier, "load banner");
  276. ExecuteWithDelay(1f, () =>
  277. {
  278. var placement = MaxSdkUtils.GetStringFromDictionary(BannerPlacements, adUnitIdentifier);
  279. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnBannerAdLoadedEvent", adUnitIdentifier, placement));
  280. MaxSdkCallbacks.ForwardEvent(eventProps);
  281. });
  282. }
  283. /// <summary>
  284. /// Set the banner placement for an ad unit identifier to tie the future ad events to.
  285. /// </summary>
  286. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the placement for. Must not be null.</param>
  287. /// <param name="placement">Placement to set</param>
  288. public static void SetBannerPlacement(string adUnitIdentifier, string placement)
  289. {
  290. BannerPlacements[adUnitIdentifier] = placement;
  291. MaxSdkLogger.UserDebug("Setting banner placement to '" + placement + "' for ad unit id '" + adUnitIdentifier + "'");
  292. }
  293. /// <summary>
  294. /// Starts or resumes auto-refreshing of the banner for the given ad unit identifier.
  295. /// </summary>
  296. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to start auto-refresh. Must not be null.</param>
  297. public static void StartBannerAutoRefresh(string adUnitIdentifier)
  298. {
  299. MaxSdkLogger.UserDebug("Starting banner auto refresh.");
  300. }
  301. /// <summary>
  302. /// Pauses auto-refreshing of the banner for the given ad unit identifier.
  303. /// </summary>
  304. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to stop auto-refresh. Must not be null.</param>
  305. public static void StopBannerAutoRefresh(string adUnitIdentifier)
  306. {
  307. MaxSdkLogger.UserDebug("Stopping banner auto refresh.");
  308. }
  309. /// <summary>
  310. /// Updates the position of the banner to the new position provided.
  311. /// </summary>
  312. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to update the position. Must not be null.</param>
  313. /// <param name="bannerPosition">A new position for the banner. Must not be null.</param>
  314. public static void UpdateBannerPosition(string adUnitIdentifier, BannerPosition bannerPosition)
  315. {
  316. MaxSdkLogger.D("[AppLovin MAX] Updating banner position to '" + bannerPosition + "' for ad unit id '" + adUnitIdentifier + "'");
  317. }
  318. /// <summary>
  319. /// Updates the position of the banner to the new coordinates provided.
  320. /// </summary>
  321. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to update the position. Must not be null.</param>
  322. /// <param name="x">The X coordinate (horizontal position) of the banner relative to the top left corner of the screen.</param>
  323. /// <param name="y">The Y coordinate (vertical position) of the banner relative to the top left corner of the screen.</param>
  324. /// <seealso cref="GetBannerLayout">
  325. /// 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.
  326. /// </seealso>
  327. public static void UpdateBannerPosition(string adUnitIdentifier, float x, float y)
  328. {
  329. MaxSdkLogger.UserDebug("Updating banner position to '(" + x + "," + y + ")");
  330. }
  331. /// <summary>
  332. /// Overrides the width of the banner in points/dp.
  333. /// </summary>
  334. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to override the width for. Must not be null.</param>
  335. /// <param name="width">The desired width of the banner in points/dp</param>
  336. public static void SetBannerWidth(string adUnitIdentifier, float width)
  337. {
  338. // NOTE: Will implement in a future release
  339. MaxSdkLogger.D("[AppLovin MAX] Set banner width to '" + width + "' for ad unit id '" + adUnitIdentifier + "'");
  340. }
  341. /// <summary>
  342. /// Show banner at a position determined by the 'CreateBanner' call.
  343. /// </summary>
  344. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to show. Must not be null.</param>
  345. public static void ShowBanner(string adUnitIdentifier)
  346. {
  347. ValidateAdUnitIdentifier(adUnitIdentifier, "show banner");
  348. if (!IsAdUnitRequested(adUnitIdentifier))
  349. {
  350. MaxSdkLogger.UserWarning("Banner '" + adUnitIdentifier + "' was not created, can not show it");
  351. }
  352. else
  353. {
  354. GameObject stubBanner;
  355. if (StubBanners.TryGetValue(adUnitIdentifier, out stubBanner))
  356. {
  357. stubBanner.SetActive(true);
  358. }
  359. }
  360. }
  361. /// <summary>
  362. /// Remove banner from the ad view and destroy it.
  363. /// </summary>
  364. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to destroy. Must not be null.</param>
  365. public static void DestroyBanner(string adUnitIdentifier)
  366. {
  367. ValidateAdUnitIdentifier(adUnitIdentifier, "destroy banner");
  368. GameObject stubBanner;
  369. if (StubBanners.TryGetValue(adUnitIdentifier, out stubBanner))
  370. {
  371. Object.Destroy(stubBanner);
  372. StubBanners.Remove(adUnitIdentifier);
  373. }
  374. }
  375. /// <summary>
  376. /// Hide banner.
  377. /// </summary>
  378. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to hide. Must not be null.</param>
  379. public static void HideBanner(string adUnitIdentifier)
  380. {
  381. ValidateAdUnitIdentifier(adUnitIdentifier, "hide banner");
  382. GameObject stubBanner;
  383. if (StubBanners.TryGetValue(adUnitIdentifier, out stubBanner))
  384. {
  385. stubBanner.SetActive(false);
  386. }
  387. }
  388. /// <summary>
  389. /// Set non-transparent background color for banners to be fully functional.
  390. /// </summary>
  391. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set background color for. Must not be null.</param>
  392. /// <param name="color">A background color to set for the ad. Must not be null.</param>
  393. public static void SetBannerBackgroundColor(string adUnitIdentifier, Color color)
  394. {
  395. ValidateAdUnitIdentifier(adUnitIdentifier, "set background color");
  396. GameObject stubBanner;
  397. if (StubBanners.TryGetValue(adUnitIdentifier, out stubBanner))
  398. {
  399. stubBanner.GetComponentInChildren<Image>().color = color;
  400. }
  401. }
  402. /// <summary>
  403. /// Set an extra parameter for the banner ad.
  404. /// </summary>
  405. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the extra parameter for. Must not be null.</param>
  406. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  407. /// <param name="value">The value for the extra parameter.</param>
  408. public static void SetBannerExtraParameter(string adUnitIdentifier, string key, string value)
  409. {
  410. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner extra parameter");
  411. }
  412. /// <summary>
  413. /// Set a local extra parameter for the banner ad.
  414. /// </summary>
  415. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the local extra parameter for. Must not be null.</param>
  416. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  417. /// <param name="value">The value for the local extra parameter.</param>
  418. public static void SetBannerLocalExtraParameter(string adUnitIdentifier, string key, object value)
  419. {
  420. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner local extra parameter");
  421. }
  422. /// <summary>
  423. /// 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.
  424. /// </summary>
  425. /// <param name="adUnitIdentifier">Banner ad unit identifier of the banner to set the custom data for. Must not be null.</param>
  426. /// <param name="customData">The custom data to be set.</param>
  427. public static void SetBannerCustomData(string adUnitIdentifier, string customData)
  428. {
  429. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner custom data");
  430. }
  431. /// <summary>
  432. /// 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)"/>,
  433. /// the banner is placed within the safe area of the screen. This returns the absolute position of the banner on screen.
  434. /// </summary>
  435. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to get the position on screen. Must not be null.</param>
  436. /// <returns>A <see cref="Rect"/> representing the banner position on screen.</returns>
  437. public static Rect GetBannerLayout(string adUnitIdentifier)
  438. {
  439. ValidateAdUnitIdentifier(adUnitIdentifier, "get banner layout");
  440. return Rect.zero;
  441. }
  442. #endregion
  443. #region MRECs
  444. /// <summary>
  445. /// Create a new MREC.
  446. /// </summary>
  447. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to create. Must not be null.</param>
  448. /// <param name="mrecPosition">MREC position. Must not be null.</param>
  449. public static void CreateMRec(string adUnitIdentifier, AdViewPosition mrecPosition)
  450. {
  451. ValidateAdUnitIdentifier(adUnitIdentifier, "create MREC");
  452. RequestAdUnit(adUnitIdentifier);
  453. ExecuteWithDelay(1f, () =>
  454. {
  455. var placement = MaxSdkUtils.GetStringFromDictionary(MRecPlacements, adUnitIdentifier);
  456. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnMRecAdLoadedEvent", adUnitIdentifier, placement));
  457. MaxSdkCallbacks.ForwardEvent(eventProps);
  458. });
  459. }
  460. /// <summary>
  461. /// Create a new MREC with a custom position.
  462. /// </summary>
  463. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to create. Must not be null.</param>
  464. /// <param name="x">The X coordinate (horizontal position) of the MREC relative to the top left corner of the screen.</param>
  465. /// <param name="y">The Y coordinate (vertical position) of the MREC relative to the top left corner of the screen.</param>
  466. /// <seealso cref="GetMRecLayout">
  467. /// 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.
  468. /// </seealso>
  469. public static void CreateMRec(string adUnitIdentifier, float x, float y)
  470. {
  471. ValidateAdUnitIdentifier(adUnitIdentifier, "create MREC");
  472. RequestAdUnit(adUnitIdentifier);
  473. }
  474. /// <summary>
  475. /// Load a new MREC ad.
  476. /// NOTE: The <see cref="CreateMRec()"/> method loads the first MREC ad and initiates an automated MREC refresh process.
  477. /// You only need to call this method if you pause MREC refresh.
  478. /// </summary>
  479. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to load. Must not be null.</param>
  480. public static void LoadMRec(string adUnitIdentifier)
  481. {
  482. ValidateAdUnitIdentifier(adUnitIdentifier, "load MREC");
  483. ExecuteWithDelay(1f, () =>
  484. {
  485. var placement = MaxSdkUtils.GetStringFromDictionary(MRecPlacements, adUnitIdentifier);
  486. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnMRecAdLoadedEvent", adUnitIdentifier, placement));
  487. MaxSdkCallbacks.ForwardEvent(eventProps);
  488. });
  489. }
  490. /// <summary>
  491. /// Set the MREC placement for an ad unit identifier to tie the future ad events to.
  492. /// </summary>
  493. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the placement for. Must not be null.</param>
  494. /// <param name="placement">Placement to set</param>
  495. public static void SetMRecPlacement(string adUnitIdentifier, string placement)
  496. {
  497. MRecPlacements[adUnitIdentifier] = placement;
  498. MaxSdkLogger.UserDebug("Setting MREC placement to '" + placement + "' for ad unit id '" + adUnitIdentifier + "'");
  499. }
  500. /// <summary>
  501. /// Starts or resumes auto-refreshing of the MREC for the given ad unit identifier.
  502. /// </summary>
  503. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to start auto-refresh. Must not be null.</param>
  504. public static void StartMRecAutoRefresh(string adUnitIdentifier)
  505. {
  506. MaxSdkLogger.UserDebug("Starting banner auto refresh.");
  507. }
  508. /// <summary>
  509. /// Pauses auto-refreshing of the MREC for the given ad unit identifier.
  510. /// </summary>
  511. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to stop auto-refresh. Must not be null.</param>
  512. public static void StopMRecAutoRefresh(string adUnitIdentifier)
  513. {
  514. MaxSdkLogger.UserDebug("Stopping banner auto refresh.");
  515. }
  516. /// <summary>
  517. /// Updates the position of the MREC to the new position provided.
  518. /// </summary>
  519. /// <param name="adUnitIdentifier">The ad unit identifier of the MREC for which to update the position. Must not be null.</param>
  520. /// <param name="mrecPosition">A new position for the MREC. Must not be null.</param>
  521. public static void UpdateMRecPosition(string adUnitIdentifier, AdViewPosition mrecPosition)
  522. {
  523. MaxSdkLogger.UserDebug("Updating MREC position to '" + mrecPosition + "' for ad unit id '" + adUnitIdentifier + "'");
  524. }
  525. /// <summary>
  526. /// Updates the position of the MREC to the new coordinates provided.
  527. /// </summary>
  528. /// <param name="adUnitIdentifier">The ad unit identifier of the MREC for which to update the position. Must not be null.</param>
  529. /// <param name="x">The X coordinate (horizontal position) of the MREC relative to the top left corner of the screen.</param>
  530. /// <param name="y">The Y coordinate (vertical position) of the MREC relative to the top left corner of the screen.</param>
  531. /// <seealso cref="GetMRecLayout">
  532. /// 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.
  533. /// </seealso>
  534. public static void UpdateMRecPosition(string adUnitIdentifier, float x, float y)
  535. {
  536. MaxSdkLogger.UserDebug("Updating MREC position to '(" + x + "," + y + ")");
  537. }
  538. /// <summary>
  539. /// Show MREC at a position determined by the 'CreateMRec' call.
  540. /// </summary>
  541. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to show. Must not be null.</param>
  542. public static void ShowMRec(string adUnitIdentifier)
  543. {
  544. ValidateAdUnitIdentifier(adUnitIdentifier, "show MREC");
  545. if (!IsAdUnitRequested(adUnitIdentifier))
  546. {
  547. MaxSdkLogger.UserWarning("MREC '" + adUnitIdentifier + "' was not created, can not show it");
  548. }
  549. }
  550. /// <summary>
  551. /// Remove MREC from the ad view and destroy it.
  552. /// </summary>
  553. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to destroy. Must not be null.</param>
  554. public static void DestroyMRec(string adUnitIdentifier)
  555. {
  556. ValidateAdUnitIdentifier(adUnitIdentifier, "destroy MREC");
  557. }
  558. /// <summary>
  559. /// Hide MREC.
  560. /// </summary>
  561. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to hide. Must not be null.</param>
  562. public static void HideMRec(string adUnitIdentifier)
  563. {
  564. ValidateAdUnitIdentifier(adUnitIdentifier, "hide MREC");
  565. }
  566. /// <summary>
  567. /// Set an extra parameter for the MREC ad.
  568. /// </summary>
  569. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the extra parameter for. Must not be null.</param>
  570. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  571. /// <param name="value">The value for the extra parameter.</param>
  572. public static void SetMRecExtraParameter(string adUnitIdentifier, string key, string value)
  573. {
  574. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC extra parameter");
  575. }
  576. /// <summary>
  577. /// Set a local extra parameter for the MREC ad.
  578. /// </summary>
  579. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the local extra parameter for. Must not be null.</param>
  580. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  581. /// <param name="value">The value for the local extra parameter.</param>
  582. public static void SetMRecLocalExtraParameter(string adUnitIdentifier, string key, object value)
  583. {
  584. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC local extra parameter");
  585. }
  586. /// <summary>
  587. /// 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.
  588. /// </summary>
  589. /// <param name="adUnitIdentifier">MREC Ad unit identifier of the banner to set the custom data for. Must not be null.</param>
  590. /// <param name="customData">The custom data to be set.</param>
  591. public static void SetMRecCustomData(string adUnitIdentifier, string customData)
  592. {
  593. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC custom data");
  594. }
  595. /// <summary>
  596. /// 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)"/>,
  597. /// the MREC is placed within the safe area of the screen. This returns the absolute position of the MREC on screen.
  598. /// </summary>
  599. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to get the position on screen. Must not be null.</param>
  600. /// <returns>A <see cref="Rect"/> representing the banner position on screen.</returns>
  601. public static Rect GetMRecLayout(string adUnitIdentifier)
  602. {
  603. ValidateAdUnitIdentifier(adUnitIdentifier, "get MREC layout");
  604. return Rect.zero;
  605. }
  606. #endregion
  607. #region Interstitials
  608. /// <summary>
  609. /// Start loading an interstitial.
  610. /// </summary>
  611. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load</param>
  612. public static void LoadInterstitial(string adUnitIdentifier)
  613. {
  614. ValidateAdUnitIdentifier(adUnitIdentifier, "load interstitial");
  615. RequestAdUnit(adUnitIdentifier);
  616. ExecuteWithDelay(1f, () =>
  617. {
  618. AddReadyAdUnit(adUnitIdentifier);
  619. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnInterstitialLoadedEvent", adUnitIdentifier));
  620. MaxSdkCallbacks.ForwardEvent(eventProps);
  621. });
  622. }
  623. /// <summary>
  624. /// Check if interstitial ad is loaded and ready to be displayed.
  625. /// </summary>
  626. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load</param>
  627. /// <returns>True if the ad is ready to be displayed</returns>
  628. public static bool IsInterstitialReady(string adUnitIdentifier)
  629. {
  630. ValidateAdUnitIdentifier(adUnitIdentifier, "check interstitial loaded");
  631. if (!IsAdUnitRequested(adUnitIdentifier))
  632. {
  633. MaxSdkLogger.UserWarning("Interstitial '" + adUnitIdentifier +
  634. "' was not requested, can not check if it is loaded");
  635. return false;
  636. }
  637. return IsAdUnitReady(adUnitIdentifier);
  638. }
  639. /// <summary>
  640. /// 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.
  641. /// </summary>
  642. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load</param>
  643. /// <param name="placement">The placement to tie the showing ad's events to</param>
  644. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  645. public static void ShowInterstitial(string adUnitIdentifier, string placement = null, string customData = null)
  646. {
  647. ValidateAdUnitIdentifier(adUnitIdentifier, "show interstitial");
  648. if (!IsAdUnitRequested(adUnitIdentifier))
  649. {
  650. MaxSdkLogger.UserWarning(
  651. "Interstitial '" + adUnitIdentifier + "' was not requested, can not show it");
  652. return;
  653. }
  654. if (!IsInterstitialReady(adUnitIdentifier))
  655. {
  656. MaxSdkLogger.UserWarning("Interstitial '" + adUnitIdentifier + "' is not ready, please check IsInterstitialReady() before showing.");
  657. return;
  658. }
  659. RemoveReadyAdUnit(adUnitIdentifier);
  660. if (_showStubAds)
  661. {
  662. ShowStubInterstitial(adUnitIdentifier, placement);
  663. }
  664. }
  665. private static void ShowStubInterstitial(string adUnitIdentifier, string placement)
  666. {
  667. #if UNITY_EDITOR
  668. var prefabPath = MaxSdkUtils.GetAssetPathForExportPath("MaxSdk/Prefabs/Interstitial.prefab");
  669. var interstitialPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  670. var stubInterstitial = Object.Instantiate(interstitialPrefab, Vector3.zero, Quaternion.identity);
  671. var interstitialText = GameObject.Find("MaxInterstitialTitle").GetComponent<Text>();
  672. var closeButton = GameObject.Find("MaxInterstitialCloseButton").GetComponent<Button>();
  673. Object.DontDestroyOnLoad(stubInterstitial);
  674. interstitialText.text += ":\n" + adUnitIdentifier;
  675. closeButton.onClick.AddListener(() =>
  676. {
  677. var adHiddenEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnInterstitialHiddenEvent", adUnitIdentifier, placement));
  678. MaxSdkCallbacks.ForwardEvent(adHiddenEventProps);
  679. Object.Destroy(stubInterstitial);
  680. });
  681. var adDisplayedEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnInterstitialDisplayedEvent", adUnitIdentifier, placement));
  682. MaxSdkCallbacks.ForwardEvent(adDisplayedEventProps);
  683. #endif
  684. }
  685. /// <summary>
  686. /// Set an extra parameter for the ad.
  687. /// </summary>
  688. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to set the extra parameter for.</param>
  689. /// <param name="key">The key for the extra parameter.</param>
  690. /// <param name="value">The value for the extra parameter.</param>
  691. public static void SetInterstitialExtraParameter(string adUnitIdentifier, string key, string value)
  692. {
  693. ValidateAdUnitIdentifier(adUnitIdentifier, "set interstitial extra parameter");
  694. }
  695. /// <summary>
  696. /// Set a local extra parameter for the ad.
  697. /// </summary>
  698. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to set the local extra parameter for.</param>
  699. /// <param name="key">The key for the local extra parameter.</param>
  700. /// <param name="value">The value for the local extra parameter.</param>
  701. public static void SetInterstitialLocalExtraParameter(string adUnitIdentifier, string key, object value)
  702. {
  703. ValidateAdUnitIdentifier(adUnitIdentifier, "set interstitial local extra parameter");
  704. }
  705. #endregion
  706. #region App Open Ads
  707. /// <summary>
  708. /// Start loading an app open ad.
  709. /// </summary>
  710. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  711. public static void LoadAppOpenAd(string adUnitIdentifier)
  712. {
  713. ValidateAdUnitIdentifier(adUnitIdentifier, "load app open ad");
  714. RequestAdUnit(adUnitIdentifier);
  715. ExecuteWithDelay(1f, () =>
  716. {
  717. AddReadyAdUnit(adUnitIdentifier);
  718. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnAppOpenAdLoadedEvent", adUnitIdentifier));
  719. MaxSdkCallbacks.ForwardEvent(eventProps);
  720. });
  721. }
  722. /// <summary>
  723. /// Check if app open ad ad is loaded and ready to be displayed.
  724. /// </summary>
  725. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  726. /// <returns>True if the ad is ready to be displayed</returns>
  727. public static bool IsAppOpenAdReady(string adUnitIdentifier)
  728. {
  729. ValidateAdUnitIdentifier(adUnitIdentifier, "check app open ad loaded");
  730. if (!IsAdUnitRequested(adUnitIdentifier))
  731. {
  732. MaxSdkLogger.UserWarning("App Open Ad '" + adUnitIdentifier +
  733. "' was not requested, can not check if it is loaded");
  734. return false;
  735. }
  736. return IsAdUnitReady(adUnitIdentifier);
  737. }
  738. /// <summary>
  739. /// 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.
  740. /// </summary>
  741. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  742. /// <param name="placement">The placement to tie the showing ad's events to</param>
  743. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  744. public static void ShowAppOpenAd(string adUnitIdentifier, string placement = null, string customData = null)
  745. {
  746. ValidateAdUnitIdentifier(adUnitIdentifier, "show app open ad");
  747. if (!IsAdUnitRequested(adUnitIdentifier))
  748. {
  749. MaxSdkLogger.UserWarning(
  750. "App Open Ad '" + adUnitIdentifier + "' was not requested, can not show it");
  751. return;
  752. }
  753. if (!IsAppOpenAdReady(adUnitIdentifier))
  754. {
  755. MaxSdkLogger.UserWarning("App Open Ad '" + adUnitIdentifier + "' is not ready, please check IsAppOpenAdReady() before showing.");
  756. return;
  757. }
  758. RemoveReadyAdUnit(adUnitIdentifier);
  759. if (_showStubAds)
  760. {
  761. ShowStubAppOpenAd(adUnitIdentifier, placement);
  762. }
  763. }
  764. private static void ShowStubAppOpenAd(string adUnitIdentifier, string placement)
  765. {
  766. #if UNITY_EDITOR
  767. var prefabPath = MaxSdkUtils.GetAssetPathForExportPath("MaxSdk/Prefabs/Interstitial.prefab");
  768. var appOpenAdPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  769. var stubAppOpenAd = Object.Instantiate(appOpenAdPrefab, Vector3.zero, Quaternion.identity);
  770. var appOpenAdText = GameObject.Find("MaxInterstitialTitle").GetComponent<Text>();
  771. var closeButton = GameObject.Find("MaxInterstitialCloseButton").GetComponent<Button>();
  772. Object.DontDestroyOnLoad(stubAppOpenAd);
  773. appOpenAdText.text = "MAX App Open Ad:\n" + adUnitIdentifier;
  774. closeButton.onClick.AddListener(() =>
  775. {
  776. var adHiddenEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnAppOpenAdHiddenEvent", adUnitIdentifier, placement));
  777. MaxSdkCallbacks.ForwardEvent(adHiddenEventProps);
  778. Object.Destroy(stubAppOpenAd);
  779. });
  780. var adDisplayedEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnAppOpenAdDisplayedEvent", adUnitIdentifier, placement));
  781. MaxSdkCallbacks.ForwardEvent(adDisplayedEventProps);
  782. #endif
  783. }
  784. /// <summary>
  785. /// Set an extra parameter for the ad.
  786. /// </summary>
  787. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to set the extra parameter for. Must not be null.</param>
  788. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  789. /// <param name="value">The value for the extra parameter.</param>
  790. public static void SetAppOpenAdExtraParameter(string adUnitIdentifier, string key, string value)
  791. {
  792. ValidateAdUnitIdentifier(adUnitIdentifier, "set app open ad extra parameter");
  793. }
  794. /// <summary>
  795. /// Set a local extra parameter for the ad.
  796. /// </summary>
  797. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to set the local extra parameter for. Must not be null.</param>
  798. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  799. /// <param name="value">The value for the local extra parameter.</param>
  800. public static void SetAppOpenAdLocalExtraParameter(string adUnitIdentifier, string key, object value)
  801. {
  802. ValidateAdUnitIdentifier(adUnitIdentifier, "set app open ad local extra parameter");
  803. }
  804. #endregion
  805. #region Rewarded
  806. /// <summary>
  807. /// Start loading an rewarded ad.
  808. /// </summary>
  809. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to load. Must not be null.</param>
  810. public static void LoadRewardedAd(string adUnitIdentifier)
  811. {
  812. ValidateAdUnitIdentifier(adUnitIdentifier, "load rewarded ad");
  813. RequestAdUnit(adUnitIdentifier);
  814. ExecuteWithDelay(1f, () =>
  815. {
  816. AddReadyAdUnit(adUnitIdentifier);
  817. var eventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnRewardedAdLoadedEvent", adUnitIdentifier));
  818. MaxSdkCallbacks.ForwardEvent(eventProps);
  819. });
  820. }
  821. /// <summary>
  822. /// Check if rewarded ad ad is loaded and ready to be displayed.
  823. /// </summary>
  824. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to load. Must not be null.</param>
  825. /// <returns>True if the ad is ready to be displayed</returns>
  826. public static bool IsRewardedAdReady(string adUnitIdentifier)
  827. {
  828. ValidateAdUnitIdentifier(adUnitIdentifier, "check rewarded ad loaded");
  829. if (!IsAdUnitRequested(adUnitIdentifier))
  830. {
  831. MaxSdkLogger.UserWarning("Rewarded ad '" + adUnitIdentifier +
  832. "' was not requested, can not check if it is loaded");
  833. return false;
  834. }
  835. return IsAdUnitReady(adUnitIdentifier);
  836. }
  837. /// <summary>
  838. /// 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.
  839. /// </summary>
  840. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load. Must not be null.</param>
  841. /// <param name="placement">The placement to tie the showing ad's events to</param>
  842. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  843. public static void ShowRewardedAd(string adUnitIdentifier, string placement = null, string customData = null)
  844. {
  845. ValidateAdUnitIdentifier(adUnitIdentifier, "show rewarded ad");
  846. if (!IsAdUnitRequested(adUnitIdentifier))
  847. {
  848. MaxSdkLogger.UserWarning("Rewarded ad '" + adUnitIdentifier +
  849. "' was not requested, can not show it");
  850. return;
  851. }
  852. if (!IsRewardedAdReady(adUnitIdentifier))
  853. {
  854. MaxSdkLogger.UserWarning("Rewarded ad '" + adUnitIdentifier + "' is not ready, please check IsRewardedAdReady() before showing.");
  855. return;
  856. }
  857. RemoveReadyAdUnit(adUnitIdentifier);
  858. if (_showStubAds)
  859. {
  860. ShowStubRewardedAd(adUnitIdentifier, placement);
  861. }
  862. }
  863. private static void ShowStubRewardedAd(string adUnitIdentifier, string placement)
  864. {
  865. #if UNITY_EDITOR
  866. var prefabPath = MaxSdkUtils.GetAssetPathForExportPath("MaxSdk/Prefabs/Rewarded.prefab");
  867. var rewardedPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  868. var stubRewardedAd = Object.Instantiate(rewardedPrefab, Vector3.zero, Quaternion.identity);
  869. var grantedReward = false;
  870. var rewardedTitle = GameObject.Find("MaxRewardTitle").GetComponent<Text>();
  871. var rewardStatus = GameObject.Find("MaxRewardStatus").GetComponent<Text>();
  872. var closeButton = GameObject.Find("MaxRewardedCloseButton").GetComponent<Button>();
  873. var rewardButton = GameObject.Find("MaxRewardButton").GetComponent<Button>();
  874. Object.DontDestroyOnLoad(stubRewardedAd);
  875. rewardedTitle.text += ":\n" + adUnitIdentifier;
  876. closeButton.onClick.AddListener(() =>
  877. {
  878. if (grantedReward)
  879. {
  880. var rewardEventPropsDict = CreateBaseEventPropsDictionary("OnRewardedAdReceivedRewardEvent", adUnitIdentifier, placement);
  881. rewardEventPropsDict["rewardLabel"] = "coins";
  882. rewardEventPropsDict["rewardAmount"] = "5";
  883. var rewardEventProps = Json.Serialize(rewardEventPropsDict);
  884. MaxSdkCallbacks.ForwardEvent(rewardEventProps);
  885. }
  886. var adHiddenEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnRewardedAdHiddenEvent", adUnitIdentifier, placement));
  887. MaxSdkCallbacks.ForwardEvent(adHiddenEventProps);
  888. Object.Destroy(stubRewardedAd);
  889. });
  890. rewardButton.onClick.AddListener(() =>
  891. {
  892. grantedReward = true;
  893. rewardStatus.text = "Reward granted. Will send reward callback on ad close.";
  894. });
  895. var adDisplayedEventProps = Json.Serialize(CreateBaseEventPropsDictionary("OnRewardedAdDisplayedEvent", adUnitIdentifier, placement));
  896. MaxSdkCallbacks.ForwardEvent(adDisplayedEventProps);
  897. #endif
  898. }
  899. /// <summary>
  900. /// Set an extra parameter for the ad.
  901. /// </summary>
  902. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to set the extra parameter for. Must not be null.</param>
  903. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  904. /// <param name="value">The value for the extra parameter.</param>
  905. public static void SetRewardedAdExtraParameter(string adUnitIdentifier, string key, string value)
  906. {
  907. ValidateAdUnitIdentifier(adUnitIdentifier, "set rewarded extra parameter");
  908. }
  909. /// <summary>
  910. /// Set a local extra parameter for the ad.
  911. /// </summary>
  912. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to set the local extra parameter for. Must not be null.</param>
  913. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  914. /// <param name="value">The value for the local extra parameter.</param>
  915. public static void SetRewardedAdLocalExtraParameter(string adUnitIdentifier, string key, object value)
  916. {
  917. ValidateAdUnitIdentifier(adUnitIdentifier, "set rewarded local extra parameter");
  918. }
  919. #endregion
  920. #region Event Tracking
  921. /// <summary>
  922. /// Track an event using AppLovin.
  923. /// </summary>
  924. /// <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>
  925. /// <param name="parameters">A dictionary containing key-value pairs further describing this event.</param>
  926. public static void TrackEvent(string name, IDictionary<string, string> parameters = null) { }
  927. #endregion
  928. #region Settings
  929. private static bool _isMuted;
  930. /// <summary>
  931. /// Set whether to begin video ads in a muted state or not.
  932. ///
  933. /// Please call this method after the SDK has initialized.
  934. /// </summary>
  935. /// <param name="muted"><c>true</c> if video ads should being in muted state.</param>
  936. public static void SetMuted(bool muted)
  937. {
  938. _isMuted = muted;
  939. }
  940. /// <summary>
  941. /// Whether video ads begin in a muted state or not. Defaults to <c>false</c>.
  942. ///
  943. /// Note: Returns <c>false</c> if the SDK is not initialized yet.
  944. /// </summary>
  945. /// <returns><c>true</c> if video ads begin in muted state.</returns>
  946. public static bool IsMuted()
  947. {
  948. return _isMuted;
  949. }
  950. /// <summary>
  951. /// Toggle verbose logging of AppLovin SDK. If enabled AppLovin messages will appear in standard application log. All log messages will have "AppLovinSdk" tag.
  952. /// </summary>
  953. /// <param name="enabled"><c>true</c> if verbose logging should be enabled.</param>
  954. public static void SetVerboseLogging(bool enabled)
  955. {
  956. #if UNITY_EDITOR
  957. EditorPrefs.SetBool(MaxSdkLogger.KeyVerboseLoggingEnabled, enabled);
  958. #endif
  959. }
  960. /// <summary>
  961. /// Whether or not verbose logging is enabled.
  962. /// </summary>
  963. /// <returns><c>true</c> if verbose logging is enabled.</returns>
  964. public static bool IsVerboseLoggingEnabled()
  965. {
  966. #if UNITY_EDITOR
  967. return EditorPrefs.GetBool(MaxSdkLogger.KeyVerboseLoggingEnabled, false);
  968. #else
  969. return false;
  970. #endif
  971. }
  972. /// <summary>
  973. /// Whether the creative debugger will be displayed on fullscreen ads after flipping the device screen down twice. Defaults to true.
  974. /// </summary>
  975. /// <param name="enabled"><c>true</c> if the creative debugger should be enabled.</param>
  976. public static void SetCreativeDebuggerEnabled(bool enabled) { }
  977. /// <summary>
  978. /// Enable devices to receive test ads, by passing in the advertising identifier (IDFA/GAID) of each test device.
  979. /// Refer to AppLovin logs for the IDFA/GAID of your current device.
  980. /// </summary>
  981. /// <param name="advertisingIdentifiers">String list of advertising identifiers from devices to receive test ads.</param>
  982. public static void SetTestDeviceAdvertisingIdentifiers(string[] advertisingIdentifiers)
  983. {
  984. if (IsInitialized())
  985. {
  986. MaxSdkLogger.UserError("Test Device Advertising Identifiers must be set before SDK initialization.");
  987. }
  988. }
  989. /// <summary>
  990. /// Whether or not the native AppLovin SDKs listen to exceptions. Defaults to <c>true</c>.
  991. /// </summary>
  992. /// <param name="enabled"><c>true</c> if the native AppLovin SDKs should not listen to exceptions.</param>
  993. public static void SetExceptionHandlerEnabled(bool enabled) { }
  994. /// <summary>
  995. /// Set an extra parameter to pass to the AppLovin server.
  996. /// </summary>
  997. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  998. /// <param name="value">The value for the extra parameter. May be null.</param>
  999. public static void SetExtraParameter(string key, string value)
  1000. {
  1001. HandleExtraParameter(key, value);
  1002. }
  1003. /// <summary>
  1004. /// Get the native insets in pixels for the safe area.
  1005. /// These insets are used to position ads within the safe area of the screen.
  1006. /// </summary>
  1007. public static SafeAreaInsets GetSafeAreaInsets()
  1008. {
  1009. return _safeAreaInsets;
  1010. }
  1011. #endregion
  1012. #region Internal
  1013. private static void RequestAdUnit(string adUnitId)
  1014. {
  1015. EnsureInitialized();
  1016. RequestedAdUnits.Add(adUnitId);
  1017. }
  1018. private static bool IsAdUnitRequested(string adUnitId)
  1019. {
  1020. EnsureInitialized();
  1021. return RequestedAdUnits.Contains(adUnitId);
  1022. }
  1023. private static void AddReadyAdUnit(string adUnitId)
  1024. {
  1025. EnsureInitialized();
  1026. ReadyAdUnits.Add(adUnitId);
  1027. }
  1028. private static bool IsAdUnitReady(string adUnitId)
  1029. {
  1030. EnsureInitialized();
  1031. return ReadyAdUnits.Contains(adUnitId);
  1032. }
  1033. private static void RemoveReadyAdUnit(string adUnitId)
  1034. {
  1035. ReadyAdUnits.Remove(adUnitId);
  1036. }
  1037. private static void EnsureInitialized()
  1038. {
  1039. if (_isInitialized) return;
  1040. MaxSdkLogger.UserWarning("MAX Ads SDK is not initialized by the time ad is requested. Please call Max.InitializeSdk() in your first scene");
  1041. }
  1042. private static Dictionary<string, string> CreateBaseEventPropsDictionary(string eventName, string adUnitId, string placement = null)
  1043. {
  1044. return new Dictionary<string, string>()
  1045. {
  1046. {"name", eventName},
  1047. {"adUnitId", adUnitId},
  1048. {"placement", placement ?? ""}
  1049. };
  1050. }
  1051. private static void ExecuteWithDelay(float seconds, Action action)
  1052. {
  1053. MaxEventExecutor.Instance.StartCoroutine(ExecuteAction(seconds, action));
  1054. }
  1055. private static IEnumerator ExecuteAction(float seconds, Action action)
  1056. {
  1057. yield return new WaitForSecondsRealtime(seconds);
  1058. action();
  1059. }
  1060. #endregion
  1061. #region Obsolete
  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