MaxSdkiOS.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using AOT;
  6. using UnityEngine;
  7. using AppLovinMax.ThirdParty.MiniJson;
  8. /// <summary>
  9. /// iOS AppLovin MAX Unity Plugin implementation
  10. /// </summary>
  11. public class MaxSdkiOS : MaxSdkBase
  12. {
  13. private delegate void ALUnityBackgroundCallback(string args);
  14. static MaxSdkiOS()
  15. {
  16. InitializeEventExecutor();
  17. #if UNITY_IOS
  18. _MaxSetBackgroundCallback(BackgroundCallback);
  19. #endif
  20. }
  21. #if UNITY_IOS
  22. #region Initialization
  23. [DllImport("__Internal")]
  24. private static extern void _MaxSetBackgroundCallback(ALUnityBackgroundCallback backgroundCallback);
  25. [DllImport("__Internal")]
  26. private static extern void _MaxInitializeSdk(string serializedAdUnitIds, string serializedMetaData);
  27. /// <summary>
  28. /// Initialize the default instance of AppLovin SDK.
  29. ///
  30. /// Please make sure that application's Android manifest or Info.plist includes the AppLovin SDK key.
  31. /// <param name="adUnitIds">
  32. /// 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.
  33. /// This should only be used if you have different sets of ad unit ids / credentials for the same package name.</param>
  34. /// </summary>
  35. public static void InitializeSdk(string[] adUnitIds = null)
  36. {
  37. var serializedAdUnitIds = (adUnitIds != null) ? string.Join(",", adUnitIds) : "";
  38. _MaxInitializeSdk(serializedAdUnitIds, GenerateMetaData());
  39. }
  40. [DllImport("__Internal")]
  41. private static extern bool _MaxIsInitialized();
  42. /// <summary>
  43. /// Check if the SDK has been initialized.
  44. /// </summary>
  45. /// <returns>True if SDK has been initialized</returns>
  46. public static bool IsInitialized()
  47. {
  48. return _MaxIsInitialized();
  49. }
  50. #endregion
  51. #region User Info
  52. [DllImport("__Internal")]
  53. private static extern void _MaxSetUserId(string userId);
  54. /// <summary>
  55. /// Set an identifier for the current user. This identifier will be tied to SDK events and our optional S2S postbacks.
  56. ///
  57. /// If you're using reward validation, you can optionally set an identifier to be included with currency validation postbacks.
  58. /// For example, a username or email. We'll include this in the postback when we ping your currency endpoint from our server.
  59. /// </summary>
  60. ///
  61. /// <param name="userId">The user identifier to be set. Must not be null.</param>
  62. public static void SetUserId(string userId)
  63. {
  64. _MaxSetUserId(userId);
  65. }
  66. [DllImport("__Internal")]
  67. private static extern bool _MaxSetSegmentCollection(string segmentCollectionsJson);
  68. /// <summary>
  69. /// Set the <see cref="MaxSegmentCollection"/>.
  70. /// </summary>
  71. /// <param name="segmentCollection"> The segment collection to be set. Must not be {@code null}</param>
  72. public static void SetSegmentCollection(MaxSegmentCollection segmentCollection)
  73. {
  74. _MaxSetSegmentCollection(JsonUtility.ToJson(segmentCollection));
  75. }
  76. #endregion
  77. #region MAX
  78. [DllImport("__Internal")]
  79. private static extern string _MaxGetAvailableMediatedNetworks();
  80. /// <summary>
  81. /// Returns the list of available mediation networks.
  82. ///
  83. /// Please call this method after the SDK has initialized.
  84. /// </summary>
  85. public static List<MaxSdkBase.MediatedNetworkInfo> GetAvailableMediatedNetworks()
  86. {
  87. var serializedNetworks = _MaxGetAvailableMediatedNetworks();
  88. return MaxSdkUtils.PropsStringsToList<MaxSdkBase.MediatedNetworkInfo>(serializedNetworks);
  89. }
  90. [DllImport("__Internal")]
  91. private static extern void _MaxShowMediationDebugger();
  92. /// <summary>
  93. /// Present the mediation debugger UI.
  94. /// This debugger tool provides the status of your integration for each third-party ad network.
  95. ///
  96. /// Please call this method after the SDK has initialized.
  97. /// </summary>
  98. public static void ShowMediationDebugger()
  99. {
  100. _MaxShowMediationDebugger();
  101. }
  102. [DllImport("__Internal")]
  103. private static extern void _MaxShowCreativeDebugger();
  104. /// <summary>
  105. /// Present the creative debugger UI.
  106. /// This debugger tool provides information for recently displayed ads.
  107. ///
  108. /// Please call this method after the SDK has initialized.
  109. /// </summary>
  110. public static void ShowCreativeDebugger()
  111. {
  112. _MaxShowCreativeDebugger();
  113. }
  114. [DllImport("__Internal")]
  115. private static extern string _MaxGetAdValue(string adUnitIdentifier, string key);
  116. /// <summary>
  117. /// Returns the arbitrary ad value for a given ad unit identifier with key. Returns null if no ad is loaded.
  118. /// </summary>
  119. /// <param name="adUnitIdentifier">Ad unit identifier for which to get the ad value for. Must not be null.</param>
  120. /// <param name="key">Ad value key. Must not be null.</param>
  121. /// <returns>Arbitrary ad value for a given key, or null if no ad is loaded.</returns>
  122. public static string GetAdValue(string adUnitIdentifier, string key)
  123. {
  124. var value = _MaxGetAdValue(adUnitIdentifier, key);
  125. if (string.IsNullOrEmpty(value)) return null;
  126. return value;
  127. }
  128. #endregion
  129. #region Privacy
  130. /// <summary>
  131. /// Get the SDK configuration for this user.
  132. ///
  133. /// Note: This method should be called only after SDK has been initialized.
  134. /// </summary>
  135. [DllImport("__Internal")]
  136. private static extern string _MaxGetSdkConfiguration();
  137. public static SdkConfiguration GetSdkConfiguration()
  138. {
  139. var sdkConfigurationStr = _MaxGetSdkConfiguration();
  140. var sdkConfigurationDict = Json.Deserialize(sdkConfigurationStr) as Dictionary<string, object>;
  141. return SdkConfiguration.Create(sdkConfigurationDict);
  142. }
  143. [DllImport("__Internal")]
  144. private static extern void _MaxSetHasUserConsent(bool hasUserConsent);
  145. /// <summary>
  146. /// Set whether or not user has provided consent for information sharing with AppLovin and other providers.
  147. /// </summary>
  148. /// <param name="hasUserConsent"><c>true</c> if the user has provided consent for information sharing with AppLovin. <c>false</c> by default.</param>
  149. public static void SetHasUserConsent(bool hasUserConsent)
  150. {
  151. _MaxSetHasUserConsent(hasUserConsent);
  152. }
  153. [DllImport("__Internal")]
  154. private static extern bool _MaxHasUserConsent();
  155. /// <summary>
  156. /// Check if user has provided consent for information sharing with AppLovin and other providers.
  157. /// </summary>
  158. /// <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>
  159. public static bool HasUserConsent()
  160. {
  161. return _MaxHasUserConsent();
  162. }
  163. [DllImport("__Internal")]
  164. private static extern bool _MaxIsUserConsentSet();
  165. /// <summary>
  166. /// Check if user has set consent for information sharing.
  167. /// </summary>
  168. /// <returns><c>true</c> if user has set a value of consent for information sharing.</returns>
  169. public static bool IsUserConsentSet()
  170. {
  171. return _MaxIsUserConsentSet();
  172. }
  173. [DllImport("__Internal")]
  174. private static extern void _MaxSetDoNotSell(bool doNotSell);
  175. /// <summary>
  176. /// Set whether or not user has opted out of the sale of their personal information.
  177. /// </summary>
  178. /// <param name="doNotSell"><c>true</c> if the user has opted out of the sale of their personal information.</param>
  179. public static void SetDoNotSell(bool doNotSell)
  180. {
  181. _MaxSetDoNotSell(doNotSell);
  182. }
  183. [DllImport("__Internal")]
  184. private static extern bool _MaxIsDoNotSell();
  185. /// <summary>
  186. /// Check if the user has opted out of the sale of their personal information.
  187. /// </summary>
  188. /// <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>
  189. public static bool IsDoNotSell()
  190. {
  191. return _MaxIsDoNotSell();
  192. }
  193. [DllImport("__Internal")]
  194. private static extern bool _MaxIsDoNotSellSet();
  195. /// <summary>
  196. /// Check if the user has set the option to sell their personal information
  197. /// </summary>
  198. /// <returns><c>true</c> if user has chosen an option to sell their personal information.</returns>
  199. public static bool IsDoNotSellSet()
  200. {
  201. return _MaxIsDoNotSellSet();
  202. }
  203. #endregion
  204. #region Banners
  205. [DllImport("__Internal")]
  206. private static extern void _MaxCreateBanner(string adUnitIdentifier, string bannerPosition, bool isAdaptive);
  207. [DllImport("__Internal")]
  208. private static extern void _MaxCreateBannerXY(string adUnitIdentifier, float x, float y, bool isAdaptive);
  209. /// <summary>
  210. /// Create a new banner.
  211. /// </summary>
  212. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to create. Must not be null.</param>
  213. /// <param name="configuration">The configuration for the banner</param>
  214. public static void CreateBanner(string adUnitIdentifier, AdViewConfiguration configuration)
  215. {
  216. ValidateAdUnitIdentifier(adUnitIdentifier, "create banner");
  217. if (configuration.UseCoordinates)
  218. {
  219. _MaxCreateBannerXY(adUnitIdentifier, configuration.XCoordinate, configuration.YCoordinate, configuration.IsAdaptive);
  220. }
  221. else
  222. {
  223. _MaxCreateBanner(adUnitIdentifier, configuration.Position.ToSnakeCaseString(), configuration.IsAdaptive);
  224. }
  225. }
  226. [DllImport("__Internal")]
  227. private static extern void _MaxLoadBanner(string adUnitIdentifier);
  228. /// <summary>
  229. /// Load a new banner ad.
  230. /// NOTE: The <see cref="CreateBanner()"/> method loads the first banner ad and initiates an automated banner refresh process.
  231. /// You only need to call this method if you pause banner refresh.
  232. /// </summary>
  233. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to load. Must not be null.</param>
  234. public static void LoadBanner(string adUnitIdentifier)
  235. {
  236. ValidateAdUnitIdentifier(adUnitIdentifier, "load banner");
  237. _MaxLoadBanner(adUnitIdentifier);
  238. }
  239. [DllImport("__Internal")]
  240. private static extern void _MaxSetBannerPlacement(string adUnitIdentifier, string placement);
  241. /// <summary>
  242. /// Set the banner placement for an ad unit identifier to tie the future ad events to.
  243. /// </summary>
  244. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the placement for. Must not be null.</param>
  245. /// <param name="placement">Placement to set</param>
  246. public static void SetBannerPlacement(string adUnitIdentifier, string placement)
  247. {
  248. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner placement");
  249. _MaxSetBannerPlacement(adUnitIdentifier, placement);
  250. }
  251. [DllImport("__Internal")]
  252. private static extern void _MaxStartBannerAutoRefresh(string adUnitIdentifier);
  253. /// <summary>
  254. /// Starts or resumes auto-refreshing of the banner for the given ad unit identifier.
  255. /// </summary>
  256. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to start auto-refresh. Must not be null.</param>
  257. public static void StartBannerAutoRefresh(string adUnitIdentifier)
  258. {
  259. ValidateAdUnitIdentifier(adUnitIdentifier, "start banner auto-refresh");
  260. _MaxStartBannerAutoRefresh(adUnitIdentifier);
  261. }
  262. [DllImport("__Internal")]
  263. private static extern void _MaxStopBannerAutoRefresh(string adUnitIdentifeir);
  264. /// <summary>
  265. /// Pauses auto-refreshing of the banner for the given ad unit identifier.
  266. /// </summary>
  267. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to stop auto-refresh. Must not be null.</param>
  268. public static void StopBannerAutoRefresh(string adUnitIdentifier)
  269. {
  270. ValidateAdUnitIdentifier(adUnitIdentifier, "stop banner auto-refresh");
  271. _MaxStopBannerAutoRefresh(adUnitIdentifier);
  272. }
  273. [DllImport("__Internal")]
  274. private static extern void _MaxUpdateBannerPosition(string adUnitIdentifier, string bannerPosition);
  275. /// <summary>
  276. /// Updates the position of the banner to the new position provided.
  277. /// </summary>
  278. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to update the position. Must not be null.</param>
  279. /// <param name="bannerPosition">A new position for the banner. Must not be null.</param>
  280. public static void UpdateBannerPosition(string adUnitIdentifier, AdViewPosition bannerPosition)
  281. {
  282. ValidateAdUnitIdentifier(adUnitIdentifier, "update banner position");
  283. _MaxUpdateBannerPosition(adUnitIdentifier, bannerPosition.ToSnakeCaseString());
  284. }
  285. [DllImport("__Internal")]
  286. private static extern void _MaxUpdateBannerPositionXY(string adUnitIdentifier, float x, float y);
  287. /// <summary>
  288. /// Updates the position of the banner to the new coordinates provided.
  289. /// </summary>
  290. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to update the position. Must not be null.</param>
  291. /// <param name="x">The X coordinate (horizontal position) of the banner relative to the top left corner of the screen.</param>
  292. /// <param name="y">The Y coordinate (vertical position) of the banner relative to the top left corner of the screen.</param>
  293. /// <seealso cref="GetBannerLayout">
  294. /// 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.
  295. /// </seealso>
  296. public static void UpdateBannerPosition(string adUnitIdentifier, float x, float y)
  297. {
  298. ValidateAdUnitIdentifier(adUnitIdentifier, "update banner position");
  299. _MaxUpdateBannerPositionXY(adUnitIdentifier, x, y);
  300. }
  301. [DllImport("__Internal")]
  302. private static extern void _MaxSetBannerWidth(string adUnitIdentifier, float width);
  303. /// <summary>
  304. /// Overrides the width of the banner in points.
  305. /// </summary>
  306. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to override the width for. Must not be null.</param>
  307. /// <param name="width">The desired width of the banner in points</param>
  308. public static void SetBannerWidth(string adUnitIdentifier, float width)
  309. {
  310. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner width");
  311. _MaxSetBannerWidth(adUnitIdentifier, width);
  312. }
  313. [DllImport("__Internal")]
  314. private static extern void _MaxShowBanner(string adUnitIdentifier);
  315. /// <summary>
  316. /// Show banner at a position determined by the 'CreateBanner' call.
  317. /// </summary>
  318. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to show. Must not be null.</param>
  319. public static void ShowBanner(string adUnitIdentifier)
  320. {
  321. ValidateAdUnitIdentifier(adUnitIdentifier, "show banner");
  322. _MaxShowBanner(adUnitIdentifier);
  323. }
  324. [DllImport("__Internal")]
  325. private static extern void _MaxDestroyBanner(string adUnitIdentifier);
  326. /// <summary>
  327. /// Remove banner from the ad view and destroy it.
  328. /// </summary>
  329. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to destroy. Must not be null.</param>
  330. public static void DestroyBanner(string adUnitIdentifier)
  331. {
  332. ValidateAdUnitIdentifier(adUnitIdentifier, "destroy banner");
  333. _MaxDestroyBanner(adUnitIdentifier);
  334. }
  335. [DllImport("__Internal")]
  336. private static extern void _MaxHideBanner(string adUnitIdentifier);
  337. /// <summary>
  338. /// Hide banner.
  339. /// </summary>
  340. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to hide. Must not be null.</param>
  341. public static void HideBanner(string adUnitIdentifier)
  342. {
  343. ValidateAdUnitIdentifier(adUnitIdentifier, "hide banner");
  344. _MaxHideBanner(adUnitIdentifier);
  345. }
  346. [DllImport("__Internal")]
  347. private static extern void _MaxSetBannerBackgroundColor(string adUnitIdentifier, string hexColorCodeString);
  348. /// <summary>
  349. /// Set non-transparent background color for banners to be fully functional.
  350. /// </summary>
  351. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set background color for. Must not be null.</param>
  352. /// <param name="color">A background color to set for the ad</param>
  353. public static void SetBannerBackgroundColor(string adUnitIdentifier, Color color)
  354. {
  355. ValidateAdUnitIdentifier(adUnitIdentifier, "set background color");
  356. _MaxSetBannerBackgroundColor(adUnitIdentifier, MaxSdkUtils.ParseColor(color));
  357. }
  358. [DllImport("__Internal")]
  359. private static extern void _MaxSetBannerExtraParameter(string adUnitIdentifier, string key, string value);
  360. /// <summary>
  361. /// Set an extra parameter for the banner ad.
  362. /// </summary>
  363. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the extra parameter for. Must not be null.</param>
  364. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  365. /// <param name="value">The value for the extra parameter.</param>
  366. public static void SetBannerExtraParameter(string adUnitIdentifier, string key, string value)
  367. {
  368. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner extra parameter");
  369. _MaxSetBannerExtraParameter(adUnitIdentifier, key, value);
  370. }
  371. [DllImport("__Internal")]
  372. private static extern void _MaxSetBannerLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  373. [DllImport("__Internal")]
  374. private static extern void _MaxSetBannerLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  375. /// <summary>
  376. /// Set a local extra parameter for the banner ad.
  377. /// </summary>
  378. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the local extra parameter for. Must not be null.</param>
  379. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  380. /// <param name="value">The value for the local extra parameter. Accepts the following types: <see cref="IntPtr"/>, <c>null</c>, <c>IList</c>, <c>IDictionary</c>, <c>string</c>, primitive types</param>
  381. public static void SetBannerLocalExtraParameter(string adUnitIdentifier, string key, object value)
  382. {
  383. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner local extra parameter");
  384. if (value == null || value is IntPtr)
  385. {
  386. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  387. _MaxSetBannerLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  388. }
  389. else
  390. {
  391. _MaxSetBannerLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  392. }
  393. }
  394. [DllImport("__Internal")]
  395. private static extern void _MaxSetBannerCustomData(string adUnitIdentifier, string customData);
  396. /// <summary>
  397. /// 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.
  398. /// </summary>
  399. /// <param name="adUnitIdentifier">Banner ad unit identifier of the banner to set the custom data for. Must not be null.</param>
  400. /// <param name="customData">The custom data to be set.</param>
  401. public static void SetBannerCustomData(string adUnitIdentifier, string customData)
  402. {
  403. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner custom data");
  404. _MaxSetBannerCustomData(adUnitIdentifier, customData);
  405. }
  406. [DllImport("__Internal")]
  407. private static extern string _MaxGetBannerLayout(string adUnitIdentifier);
  408. /// <summary>
  409. /// 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)"/>,
  410. /// the banner is placed within the safe area of the screen. This returns the absolute position of the banner on screen.
  411. /// </summary>
  412. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to get the position on screen. Must not be null.</param>
  413. /// <returns>A <see cref="Rect"/> representing the banner position on screen.</returns>
  414. public static Rect GetBannerLayout(string adUnitIdentifier)
  415. {
  416. ValidateAdUnitIdentifier(adUnitIdentifier, "get banner layout");
  417. var positionRect = _MaxGetBannerLayout(adUnitIdentifier);
  418. return GetRectFromString(positionRect);
  419. }
  420. #endregion
  421. #region MRECs
  422. [DllImport("__Internal")]
  423. private static extern void _MaxCreateMRec(string adUnitIdentifier, string mrecPosition);
  424. [DllImport("__Internal")]
  425. private static extern void _MaxCreateMRecXY(string adUnitIdentifier, float x, float y);
  426. /// <summary>
  427. /// Create a new MREC.
  428. /// </summary>
  429. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to create. Must not be null.</param>
  430. /// <param name="configuration">The configuration for the MREC.</param>
  431. public static void CreateMRec(string adUnitIdentifier, AdViewConfiguration configuration)
  432. {
  433. ValidateAdUnitIdentifier(adUnitIdentifier, "create MREC");
  434. if (configuration.UseCoordinates)
  435. {
  436. _MaxCreateMRecXY(adUnitIdentifier, configuration.XCoordinate, configuration.YCoordinate);
  437. }
  438. else
  439. {
  440. _MaxCreateMRec(adUnitIdentifier, configuration.Position.ToSnakeCaseString());
  441. }
  442. }
  443. [DllImport("__Internal")]
  444. private static extern void _MaxLoadMRec(string adUnitIdentifier);
  445. /// <summary>
  446. /// Load a new MREC ad.
  447. /// NOTE: The <see cref="CreateMRec()"/> method loads the first MREC ad and initiates an automated MREC refresh process.
  448. /// You only need to call this method if you pause MREC refresh.
  449. /// </summary>
  450. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to load. Must not be null.</param>
  451. public static void LoadMRec(string adUnitIdentifier)
  452. {
  453. ValidateAdUnitIdentifier(adUnitIdentifier, "load MREC");
  454. _MaxLoadMRec(adUnitIdentifier);
  455. }
  456. [DllImport("__Internal")]
  457. private static extern void _MaxSetMRecPlacement(string adUnitIdentifier, string placement);
  458. /// <summary>
  459. /// Set the MREC placement for an ad unit identifier to tie the future ad events to.
  460. /// </summary>
  461. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the placement for. Must not be null.</param>
  462. /// <param name="placement">Placement to set</param>
  463. public static void SetMRecPlacement(string adUnitIdentifier, string placement)
  464. {
  465. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC placement");
  466. _MaxSetMRecPlacement(adUnitIdentifier, placement);
  467. }
  468. [DllImport("__Internal")]
  469. private static extern void _MaxStartMRecAutoRefresh(string adUnitIdentifier);
  470. /// <summary>
  471. /// Starts or resumes auto-refreshing of the MREC for the given ad unit identifier.
  472. /// </summary>
  473. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to start auto-refresh. Must not be null.</param>
  474. public static void StartMRecAutoRefresh(string adUnitIdentifier)
  475. {
  476. ValidateAdUnitIdentifier(adUnitIdentifier, "start MREC auto-refresh");
  477. _MaxStartMRecAutoRefresh(adUnitIdentifier);
  478. }
  479. [DllImport("__Internal")]
  480. private static extern void _MaxStopMRecAutoRefresh(string adUnitIdentifeir);
  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. ValidateAdUnitIdentifier(adUnitIdentifier, "stop MREC auto-refresh");
  488. _MaxStopMRecAutoRefresh(adUnitIdentifier);
  489. }
  490. [DllImport("__Internal")]
  491. private static extern void _MaxUpdateMRecPosition(string adUnitIdentifier, string mrecPosition);
  492. /// <summary>
  493. /// Updates the position of the MREC to the new position provided.
  494. /// </summary>
  495. /// <param name="adUnitIdentifier">The ad unit identifier of the MREC for which to update the position. Must not be null.</param>
  496. /// <param name="mrecPosition">A new position for the MREC. Must not be null.</param>
  497. public static void UpdateMRecPosition(string adUnitIdentifier, AdViewPosition mrecPosition)
  498. {
  499. ValidateAdUnitIdentifier(adUnitIdentifier, "update MREC position");
  500. _MaxUpdateMRecPosition(adUnitIdentifier, mrecPosition.ToSnakeCaseString());
  501. }
  502. [DllImport("__Internal")]
  503. private static extern void _MaxUpdateMRecPositionXY(string adUnitIdentifier, float x, float y);
  504. /// <summary>
  505. /// Updates the position of the MREC to the new coordinates provided.
  506. /// </summary>
  507. /// <param name="adUnitIdentifier">The ad unit identifier of the MREC for which to update the position. Must not be null.</param>
  508. /// <param name="x">The X coordinate (horizontal position) of the MREC relative to the top left corner of the screen.</param>
  509. /// <param name="y">The Y coordinate (vertical position) of the MREC relative to the top left corner of the screen.</param>
  510. /// <seealso cref="GetMRecLayout">
  511. /// 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.
  512. /// </seealso>
  513. public static void UpdateMRecPosition(string adUnitIdentifier, float x, float y)
  514. {
  515. ValidateAdUnitIdentifier(adUnitIdentifier, "update MREC position");
  516. _MaxUpdateMRecPositionXY(adUnitIdentifier, x, y);
  517. }
  518. [DllImport("__Internal")]
  519. private static extern void _MaxShowMRec(string adUnitIdentifier);
  520. /// <summary>
  521. /// Show MREC at a position determined by the 'CreateMRec' call.
  522. /// </summary>
  523. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to show. Must not be null.</param>
  524. public static void ShowMRec(string adUnitIdentifier)
  525. {
  526. ValidateAdUnitIdentifier(adUnitIdentifier, "show MREC");
  527. _MaxShowMRec(adUnitIdentifier);
  528. }
  529. [DllImport("__Internal")]
  530. private static extern void _MaxDestroyMRec(string adUnitIdentifier);
  531. /// <summary>
  532. /// Remove MREC from the ad view and destroy it.
  533. /// </summary>
  534. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to destroy. Must not be null.</param>
  535. public static void DestroyMRec(string adUnitIdentifier)
  536. {
  537. ValidateAdUnitIdentifier(adUnitIdentifier, "destroy MREC");
  538. _MaxDestroyMRec(adUnitIdentifier);
  539. }
  540. [DllImport("__Internal")]
  541. private static extern void _MaxHideMRec(string adUnitIdentifier);
  542. /// <summary>
  543. /// Hide MREC.
  544. /// </summary>
  545. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to hide. Must not be null.</param>
  546. public static void HideMRec(string adUnitIdentifier)
  547. {
  548. ValidateAdUnitIdentifier(adUnitIdentifier, "hide MREC");
  549. _MaxHideMRec(adUnitIdentifier);
  550. }
  551. [DllImport("__Internal")]
  552. private static extern void _MaxSetMRecExtraParameter(string adUnitIdentifier, string key, string value);
  553. /// <summary>
  554. /// Set an extra parameter for the MREC ad.
  555. /// </summary>
  556. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the extra parameter for. Must not be null.</param>
  557. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  558. /// <param name="value">The value for the extra parameter.</param>
  559. public static void SetMRecExtraParameter(string adUnitIdentifier, string key, string value)
  560. {
  561. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC extra parameter");
  562. _MaxSetMRecExtraParameter(adUnitIdentifier, key, value);
  563. }
  564. [DllImport("__Internal")]
  565. private static extern void _MaxSetMRecLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  566. [DllImport("__Internal")]
  567. private static extern void _MaxSetMRecLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  568. /// <summary>
  569. /// Set a local extra parameter for the MREC ad.
  570. /// </summary>
  571. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the local extra parameter for. Must not be null.</param>
  572. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  573. /// <param name="value">The value for the local extra parameter. Accepts the following types: <see cref="IntPtr"/>, <c>null</c>, <c>IList</c>, <c>IDictionary</c>, <c>string</c>, primitive types</param>
  574. public static void SetMRecLocalExtraParameter(string adUnitIdentifier, string key, object value)
  575. {
  576. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC local extra parameter");
  577. if (value == null || value is IntPtr)
  578. {
  579. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  580. _MaxSetMRecLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  581. }
  582. else
  583. {
  584. _MaxSetMRecLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  585. }
  586. }
  587. [DllImport("__Internal")]
  588. private static extern void _MaxSetMRecCustomData(string adUnitIdentifier, string value);
  589. /// <summary>
  590. /// 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.
  591. /// </summary>
  592. /// <param name="adUnitIdentifier">MREC Ad unit identifier of the banner to set the custom data for. Must not be null.</param>
  593. /// <param name="customData">The custom data to be set.</param>
  594. public static void SetMRecCustomData(string adUnitIdentifier, string customData)
  595. {
  596. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC custom data");
  597. _MaxSetMRecCustomData(adUnitIdentifier, customData);
  598. }
  599. [DllImport("__Internal")]
  600. private static extern string _MaxGetMRecLayout(string adUnitIdentifier);
  601. /// <summary>
  602. /// The MREC position on the screen. When setting the banner position via <see cref="CreateMRec(string, float, float)"/> or <see cref="UpdateMRecPosition(string, float, float)"/>,
  603. /// the banner is placed within the safe area of the screen. This returns the absolute position of the MREC on screen.
  604. /// </summary>
  605. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to get the position on screen. Must not be null.</param>
  606. /// <returns>A <see cref="Rect"/> representing the banner position on screen.</returns>
  607. public static Rect GetMRecLayout(string adUnitIdentifier)
  608. {
  609. ValidateAdUnitIdentifier(adUnitIdentifier, "get MREC layout");
  610. var positionRect = _MaxGetMRecLayout(adUnitIdentifier);
  611. return GetRectFromString(positionRect);
  612. }
  613. #endregion
  614. #region Interstitials
  615. [DllImport("__Internal")]
  616. private static extern void _MaxLoadInterstitial(string adUnitIdentifier);
  617. /// <summary>
  618. /// Start loading an interstitial.
  619. /// </summary>
  620. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load. Must not be null.</param>
  621. public static void LoadInterstitial(string adUnitIdentifier)
  622. {
  623. ValidateAdUnitIdentifier(adUnitIdentifier, "load interstitial");
  624. _MaxLoadInterstitial(adUnitIdentifier);
  625. }
  626. [DllImport("__Internal")]
  627. private static extern bool _MaxIsInterstitialReady(string adUnitIdentifier);
  628. /// <summary>
  629. /// Check if interstitial ad is loaded and ready to be displayed.
  630. /// </summary>
  631. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial ad to check if it's ready to be displayed. Must not be null.</param>
  632. /// <returns>True if the ad is ready to be displayed</returns>
  633. public static bool IsInterstitialReady(string adUnitIdentifier)
  634. {
  635. ValidateAdUnitIdentifier(adUnitIdentifier, "check interstitial loaded");
  636. return _MaxIsInterstitialReady(adUnitIdentifier);
  637. }
  638. [DllImport("__Internal")]
  639. private static extern void _MaxShowInterstitial(string adUnitIdentifier, string placement, string customData);
  640. /// <summary>
  641. /// 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.
  642. /// </summary>
  643. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load. Must not be null.</param>
  644. /// <param name="placement">The placement to tie the showing ad's events to</param>
  645. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  646. public static void ShowInterstitial(string adUnitIdentifier, string placement = null, string customData = null)
  647. {
  648. ValidateAdUnitIdentifier(adUnitIdentifier, "show interstitial");
  649. if (IsInterstitialReady(adUnitIdentifier))
  650. {
  651. _MaxShowInterstitial(adUnitIdentifier, placement, customData);
  652. }
  653. else
  654. {
  655. MaxSdkLogger.UserWarning("Not showing MAX Ads interstitial: ad not ready");
  656. }
  657. }
  658. [DllImport("__Internal")]
  659. private static extern void _MaxSetInterstitialExtraParameter(string adUnitIdentifier, string key, string value);
  660. /// <summary>
  661. /// Set an extra parameter for the ad.
  662. /// </summary>
  663. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to set the extra parameter for. Must not be null.</param>
  664. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  665. /// <param name="value">The value for the extra parameter.</param>
  666. public static void SetInterstitialExtraParameter(string adUnitIdentifier, string key, string value)
  667. {
  668. ValidateAdUnitIdentifier(adUnitIdentifier, "set interstitial extra parameter");
  669. _MaxSetInterstitialExtraParameter(adUnitIdentifier, key, value);
  670. }
  671. [DllImport("__Internal")]
  672. private static extern void _MaxSetInterstitialLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  673. [DllImport("__Internal")]
  674. private static extern void _MaxSetInterstitialLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  675. /// <summary>
  676. /// Set a local extra parameter for the ad.
  677. /// </summary>
  678. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to set the local extra parameter for. Must not be null.</param>
  679. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  680. /// <param name="value">The value for the local extra parameter. Accepts the following types: <see cref="IntPtr"/>, <c>null</c>, <c>IList</c>, <c>IDictionary</c>, <c>string</c>, primitive types</param>
  681. public static void SetInterstitialLocalExtraParameter(string adUnitIdentifier, string key, object value)
  682. {
  683. ValidateAdUnitIdentifier(adUnitIdentifier, "set interstitial local extra parameter");
  684. if (value == null || value is IntPtr)
  685. {
  686. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  687. _MaxSetInterstitialLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  688. }
  689. else
  690. {
  691. _MaxSetInterstitialLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  692. }
  693. }
  694. #endregion
  695. #region App Open Ads
  696. [DllImport("__Internal")]
  697. private static extern void _MaxLoadAppOpenAd(string adUnitIdentifier);
  698. /// <summary>
  699. /// Start loading an app open ad.
  700. /// </summary>
  701. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  702. public static void LoadAppOpenAd(string adUnitIdentifier)
  703. {
  704. ValidateAdUnitIdentifier(adUnitIdentifier, "load app open ad");
  705. _MaxLoadAppOpenAd(adUnitIdentifier);
  706. }
  707. [DllImport("__Internal")]
  708. private static extern bool _MaxIsAppOpenAdReady(string adUnitIdentifier);
  709. /// <summary>
  710. /// Check if app open ad ad is loaded and ready to be displayed.
  711. /// </summary>
  712. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad ad to check if it's ready to be displayed. Must not be null.</param>
  713. /// <returns>True if the ad is ready to be displayed</returns>
  714. public static bool IsAppOpenAdReady(string adUnitIdentifier)
  715. {
  716. ValidateAdUnitIdentifier(adUnitIdentifier, "check app open ad loaded");
  717. return _MaxIsAppOpenAdReady(adUnitIdentifier);
  718. }
  719. [DllImport("__Internal")]
  720. private static extern void _MaxShowAppOpenAd(string adUnitIdentifier, string placement, string customData);
  721. /// <summary>
  722. /// 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.
  723. /// </summary>
  724. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  725. /// <param name="placement">The placement to tie the showing ad's events to</param>
  726. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  727. public static void ShowAppOpenAd(string adUnitIdentifier, string placement = null, string customData = null)
  728. {
  729. ValidateAdUnitIdentifier(adUnitIdentifier, "show app open ad");
  730. if (IsAppOpenAdReady(adUnitIdentifier))
  731. {
  732. _MaxShowAppOpenAd(adUnitIdentifier, placement, customData);
  733. }
  734. else
  735. {
  736. MaxSdkLogger.UserWarning("Not showing MAX Ads app open ad: ad not ready");
  737. }
  738. }
  739. [DllImport("__Internal")]
  740. private static extern void _MaxSetAppOpenAdExtraParameter(string adUnitIdentifier, string key, string value);
  741. /// <summary>
  742. /// Set an extra parameter for the ad.
  743. /// </summary>
  744. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to set the extra parameter for. Must not be null.</param>
  745. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  746. /// <param name="value">The value for the extra parameter.</param>
  747. public static void SetAppOpenAdExtraParameter(string adUnitIdentifier, string key, string value)
  748. {
  749. ValidateAdUnitIdentifier(adUnitIdentifier, "set app open ad extra parameter");
  750. _MaxSetAppOpenAdExtraParameter(adUnitIdentifier, key, value);
  751. }
  752. [DllImport("__Internal")]
  753. private static extern void _MaxSetAppOpenAdLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  754. [DllImport("__Internal")]
  755. private static extern void _MaxSetAppOpenAdLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  756. /// <summary>
  757. /// Set a local extra parameter for the ad.
  758. /// </summary>
  759. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to set the local extra parameter for. Must not be null.</param>
  760. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  761. /// <param name="value">The value for the local extra parameter. Accepts the following types: <see cref="IntPtr"/>, <c>null</c>, <c>IList</c>, <c>IDictionary</c>, <c>string</c>, primitive types</param>
  762. public static void SetAppOpenAdLocalExtraParameter(string adUnitIdentifier, string key, object value)
  763. {
  764. ValidateAdUnitIdentifier(adUnitIdentifier, "set app open ad local extra parameter");
  765. if (value == null || value is IntPtr)
  766. {
  767. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  768. _MaxSetAppOpenAdLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  769. }
  770. else
  771. {
  772. _MaxSetAppOpenAdLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  773. }
  774. }
  775. #endregion
  776. #region Rewarded
  777. [DllImport("__Internal")]
  778. private static extern void _MaxLoadRewardedAd(string adUnitIdentifier);
  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. _MaxLoadRewardedAd(adUnitIdentifier);
  787. }
  788. [DllImport("__Internal")]
  789. private static extern bool _MaxIsRewardedAdReady(string adUnitIdentifier);
  790. /// <summary>
  791. /// Check if rewarded ad ad is loaded and ready to be displayed.
  792. /// </summary>
  793. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to check if it's ready to be displayed. Must not be null.</param>
  794. /// <returns>True if the ad is ready to be displayed</returns>
  795. public static bool IsRewardedAdReady(string adUnitIdentifier)
  796. {
  797. ValidateAdUnitIdentifier(adUnitIdentifier, "check rewarded ad loaded");
  798. return _MaxIsRewardedAdReady(adUnitIdentifier);
  799. }
  800. [DllImport("__Internal")]
  801. private static extern void _MaxShowRewardedAd(string adUnitIdentifier, string placement, string customData);
  802. /// <summary>
  803. /// 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.
  804. /// </summary>
  805. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load. Must not be null.</param>
  806. /// <param name="placement">The placement to tie the showing ad's events to</param>
  807. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  808. public static void ShowRewardedAd(string adUnitIdentifier, string placement = null, string customData = null)
  809. {
  810. ValidateAdUnitIdentifier(adUnitIdentifier, "show rewarded ad");
  811. if (IsRewardedAdReady(adUnitIdentifier))
  812. {
  813. _MaxShowRewardedAd(adUnitIdentifier, placement, customData);
  814. }
  815. else
  816. {
  817. MaxSdkLogger.UserWarning("Not showing MAX Ads rewarded ad: ad not ready");
  818. }
  819. }
  820. [DllImport("__Internal")]
  821. private static extern void _MaxSetRewardedAdExtraParameter(string adUnitIdentifier, string key, string value);
  822. /// <summary>
  823. /// Set an extra parameter for the ad.
  824. /// </summary>
  825. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to set the extra parameter for. Must not be null.</param>
  826. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  827. /// <param name="value">The value for the extra parameter.</param>
  828. public static void SetRewardedAdExtraParameter(string adUnitIdentifier, string key, string value)
  829. {
  830. ValidateAdUnitIdentifier(adUnitIdentifier, "set rewarded extra parameter");
  831. _MaxSetRewardedAdExtraParameter(adUnitIdentifier, key, value);
  832. }
  833. [DllImport("__Internal")]
  834. private static extern void _MaxSetRewardedAdLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  835. [DllImport("__Internal")]
  836. private static extern void _MaxSetRewardedAdLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  837. /// <summary>
  838. /// Set a local extra parameter for the ad.
  839. /// </summary>
  840. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to set the local extra parameter for. Must not be null.</param>
  841. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  842. /// <param name="value">The value for the local extra parameter. Accepts the following types: <see cref="IntPtr"/>, <c>null</c>, <c>IList</c>, <c>IDictionary</c>, <c>string</c>, primitive types</param>
  843. public static void SetRewardedAdLocalExtraParameter(string adUnitIdentifier, string key, object value)
  844. {
  845. ValidateAdUnitIdentifier(adUnitIdentifier, "set rewarded ad local extra parameter");
  846. if (value == null || value is IntPtr)
  847. {
  848. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  849. _MaxSetRewardedAdLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  850. }
  851. else
  852. {
  853. _MaxSetRewardedAdLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  854. }
  855. }
  856. #endregion
  857. #region Event Tracking
  858. [DllImport("__Internal")]
  859. private static extern void _MaxTrackEvent(string name, string parameters);
  860. /// <summary>
  861. /// Track an event using AppLovin.
  862. /// </summary>
  863. /// <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>
  864. /// <param name="parameters">A dictionary containing key-value pairs further describing this event.</param>
  865. public static void TrackEvent(string name, IDictionary<string, string> parameters = null)
  866. {
  867. _MaxTrackEvent(name, Json.Serialize(parameters));
  868. }
  869. #endregion
  870. #region Settings
  871. [DllImport("__Internal")]
  872. private static extern void _MaxSetMuted(bool muted);
  873. /// <summary>
  874. /// Set whether to begin video ads in a muted state or not.
  875. ///
  876. /// Please call this method after the SDK has initialized.
  877. /// </summary>
  878. /// <param name="muted"><c>true</c> if video ads should being in muted state.</param>
  879. public static void SetMuted(bool muted)
  880. {
  881. _MaxSetMuted(muted);
  882. }
  883. [DllImport("__Internal")]
  884. private static extern bool _MaxIsMuted();
  885. /// <summary>
  886. /// Whether video ads begin in a muted state or not. Defaults to <c>false</c>.
  887. ///
  888. /// Note: Returns <c>false</c> if the SDK is not initialized.
  889. /// </summary>
  890. /// <returns><c>true</c> if video ads begin in muted state.</returns>
  891. public static bool IsMuted()
  892. {
  893. return _MaxIsMuted();
  894. }
  895. [DllImport("__Internal")]
  896. private static extern bool _MaxSetVerboseLogging(bool enabled);
  897. /// <summary>
  898. /// Toggle verbose logging of AppLovin SDK. If enabled AppLovin messages will appear in standard application log accessible via console. All log messages will have "AppLovinSdk" tag.
  899. /// </summary>
  900. /// <param name="enabled"><c>true</c> if verbose logging should be enabled.</param>
  901. public static void SetVerboseLogging(bool enabled)
  902. {
  903. _MaxSetVerboseLogging(enabled);
  904. }
  905. [DllImport("__Internal")]
  906. private static extern bool _MaxIsVerboseLoggingEnabled();
  907. /// <summary>
  908. /// Whether or not verbose logging is enabled.
  909. /// </summary>
  910. /// <returns><c>true</c> if verbose logging is enabled.</returns>
  911. public static bool IsVerboseLoggingEnabled()
  912. {
  913. return _MaxIsVerboseLoggingEnabled();
  914. }
  915. [DllImport("__Internal")]
  916. private static extern bool _MaxSetCreativeDebuggerEnabled(bool enabled);
  917. /// <summary>
  918. /// Whether the creative debugger will be displayed on fullscreen ads after flipping the device screen down twice. Defaults to true.
  919. /// </summary>
  920. /// <param name="enabled"><c>true</c> if the creative debugger should be enabled.</param>
  921. public static void SetCreativeDebuggerEnabled(bool enabled)
  922. {
  923. _MaxSetCreativeDebuggerEnabled(enabled);
  924. }
  925. [DllImport("__Internal")]
  926. private static extern void _MaxSetTestDeviceAdvertisingIdentifiers(string[] advertisingIdentifiers, int size);
  927. /// <summary>
  928. /// Enable devices to receive test ads, by passing in the advertising identifier (IDFA/GAID) of each test device.
  929. /// Refer to AppLovin logs for the IDFA/GAID of your current device.
  930. /// </summary>
  931. /// <param name="advertisingIdentifiers">String list of advertising identifiers from devices to receive test ads.</param>
  932. public static void SetTestDeviceAdvertisingIdentifiers(string[] advertisingIdentifiers)
  933. {
  934. if (IsInitialized())
  935. {
  936. MaxSdkLogger.UserError("Test Device Advertising Identifiers must be set before SDK initialization.");
  937. return;
  938. }
  939. _MaxSetTestDeviceAdvertisingIdentifiers(advertisingIdentifiers, advertisingIdentifiers.Length);
  940. }
  941. [DllImport("__Internal")]
  942. private static extern bool _MaxSetExceptionHandlerEnabled(bool enabled);
  943. /// <summary>
  944. /// Whether or not the native AppLovin SDKs listen to exceptions. Defaults to <c>true</c>.
  945. /// </summary>
  946. /// <param name="enabled"><c>true</c> if the native AppLovin SDKs should not listen to exceptions.</param>
  947. public static void SetExceptionHandlerEnabled(bool enabled)
  948. {
  949. _MaxSetExceptionHandlerEnabled(enabled);
  950. }
  951. [DllImport("__Internal")]
  952. private static extern void _MaxSetExtraParameter(string key, string value);
  953. /// <summary>
  954. /// Set an extra parameter to pass to the AppLovin server.
  955. /// </summary>
  956. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  957. /// <param name="value">The value for the extra parameter. May be null.</param>
  958. public static void SetExtraParameter(string key, string value)
  959. {
  960. HandleExtraParameter(key, value);
  961. _MaxSetExtraParameter(key, value);
  962. }
  963. [DllImport("__Internal")]
  964. private static extern IntPtr _MaxGetSafeAreaInsets();
  965. /// <summary>
  966. /// Get the native insets in pixels for the safe area.
  967. /// These insets are used to position ads within the safe area of the screen.
  968. /// </summary>
  969. public static SafeAreaInsets GetSafeAreaInsets()
  970. {
  971. // Use an int array instead of json serialization for performance
  972. var insetsPtr = _MaxGetSafeAreaInsets();
  973. var insets = new int[4];
  974. Marshal.Copy(insetsPtr, insets, 0, 4);
  975. // Convert from points to pixels
  976. var screenDensity = MaxSdkUtils.GetScreenDensity();
  977. for (var i = 0; i < insets.Length; i++)
  978. {
  979. insets[i] *= (int) screenDensity;
  980. }
  981. return new SafeAreaInsets(insets);
  982. }
  983. #endregion
  984. #region Private
  985. [MonoPInvokeCallback(typeof(ALUnityBackgroundCallback))]
  986. internal static void BackgroundCallback(string propsStr)
  987. {
  988. HandleBackgroundCallback(propsStr);
  989. }
  990. #endregion
  991. #region Obsolete
  992. [Obsolete("This API has been deprecated and will be removed in a future release. Please use CreateBanner(string adUnitIdentifier, AdViewConfiguration configuration) instead.")]
  993. public static void CreateBanner(string adUnitIdentifier, BannerPosition bannerPosition)
  994. {
  995. // AdViewPosition and BannerPosition share identical enum values, so casting is safe
  996. CreateBanner(adUnitIdentifier, new AdViewConfiguration((AdViewPosition) bannerPosition));
  997. }
  998. [Obsolete("This API has been deprecated and will be removed in a future release. Please use CreateBanner(string adUnitIdentifier, AdViewConfiguration configuration) instead.")]
  999. public static void CreateBanner(string adUnitIdentifier, float x, float y)
  1000. {
  1001. CreateBanner(adUnitIdentifier, new AdViewConfiguration(x, y));
  1002. }
  1003. [Obsolete("This API has been deprecated and will be removed in a future release. Please use UpdateBannerPosition(string adUnitIdentifier, AdViewPosition bannerPosition) instead.")]
  1004. public static void UpdateBannerPosition(string adUnitIdentifier, BannerPosition bannerPosition)
  1005. {
  1006. // AdViewPosition and BannerPosition share identical enum values, so casting is safe
  1007. UpdateBannerPosition(adUnitIdentifier, (AdViewPosition) bannerPosition);
  1008. }
  1009. [Obsolete("This API has been deprecated and will be removed in a future release. Please use CreateMRec(string adUnitIdentifier, AdViewConfiguration configuration) instead.")]
  1010. public static void CreateMRec(string adUnitIdentifier, AdViewPosition mrecPosition)
  1011. {
  1012. CreateMRec(adUnitIdentifier, new AdViewConfiguration(mrecPosition));
  1013. }
  1014. [Obsolete("This API has been deprecated and will be removed in a future release. Please use CreateMRec(string adUnitIdentifier, AdViewConfiguration configuration) instead.")]
  1015. public static void CreateMRec(string adUnitIdentifier, float x, float y)
  1016. {
  1017. CreateMRec(adUnitIdentifier, new AdViewConfiguration(x, y));
  1018. }
  1019. [DllImport("__Internal")]
  1020. private static extern void _MaxSetSdkKey(string sdkKey);
  1021. [Obsolete("This API has been deprecated and will be removed in a future release. Please set your SDK key in the AppLovin Integration Manager.")]
  1022. public static void SetSdkKey(string sdkKey)
  1023. {
  1024. _MaxSetSdkKey(sdkKey);
  1025. 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.");
  1026. }
  1027. #endregion
  1028. #endif
  1029. }