MaxSdkiOS.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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);
  207. /// <summary>
  208. /// Create a new banner.
  209. /// </summary>
  210. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to create. Must not be null.</param>
  211. /// <param name="bannerPosition">Banner position. Must not be null.</param>
  212. public static void CreateBanner(string adUnitIdentifier, BannerPosition bannerPosition)
  213. {
  214. ValidateAdUnitIdentifier(adUnitIdentifier, "create banner");
  215. _MaxCreateBanner(adUnitIdentifier, bannerPosition.ToSnakeCaseString());
  216. }
  217. [DllImport("__Internal")]
  218. private static extern void _MaxCreateBannerXY(string adUnitIdentifier, float x, float y);
  219. /// <summary>
  220. /// Create a new banner with a custom position.
  221. /// </summary>
  222. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to create. Must not be null.</param>
  223. /// <param name="x">The X coordinate (horizontal position) of the banner relative to the top left corner of the screen.</param>
  224. /// <param name="y">The Y coordinate (vertical position) of the banner relative to the top left corner of the screen.</param>
  225. /// <seealso cref="GetBannerLayout">
  226. /// 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.
  227. /// </seealso>
  228. public static void CreateBanner(string adUnitIdentifier, float x, float y)
  229. {
  230. ValidateAdUnitIdentifier(adUnitIdentifier, "create banner");
  231. _MaxCreateBannerXY(adUnitIdentifier, x, y);
  232. }
  233. [DllImport("__Internal")]
  234. private static extern void _MaxLoadBanner(string adUnitIdentifier);
  235. /// <summary>
  236. /// Load a new banner ad.
  237. /// NOTE: The <see cref="CreateBanner()"/> method loads the first banner ad and initiates an automated banner refresh process.
  238. /// You only need to call this method if you pause banner refresh.
  239. /// </summary>
  240. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to load. Must not be null.</param>
  241. public static void LoadBanner(string adUnitIdentifier)
  242. {
  243. ValidateAdUnitIdentifier(adUnitIdentifier, "load banner");
  244. _MaxLoadBanner(adUnitIdentifier);
  245. }
  246. [DllImport("__Internal")]
  247. private static extern void _MaxSetBannerPlacement(string adUnitIdentifier, string placement);
  248. /// <summary>
  249. /// Set the banner placement for an ad unit identifier to tie the future ad events to.
  250. /// </summary>
  251. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the placement for. Must not be null.</param>
  252. /// <param name="placement">Placement to set</param>
  253. public static void SetBannerPlacement(string adUnitIdentifier, string placement)
  254. {
  255. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner placement");
  256. _MaxSetBannerPlacement(adUnitIdentifier, placement);
  257. }
  258. [DllImport("__Internal")]
  259. private static extern void _MaxStartBannerAutoRefresh(string adUnitIdentifier);
  260. /// <summary>
  261. /// Starts or resumes auto-refreshing of the banner for the given ad unit identifier.
  262. /// </summary>
  263. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to start auto-refresh. Must not be null.</param>
  264. public static void StartBannerAutoRefresh(string adUnitIdentifier)
  265. {
  266. ValidateAdUnitIdentifier(adUnitIdentifier, "start banner auto-refresh");
  267. _MaxStartBannerAutoRefresh(adUnitIdentifier);
  268. }
  269. [DllImport("__Internal")]
  270. private static extern void _MaxStopBannerAutoRefresh(string adUnitIdentifeir);
  271. /// <summary>
  272. /// Pauses auto-refreshing of the banner for the given ad unit identifier.
  273. /// </summary>
  274. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to stop auto-refresh. Must not be null.</param>
  275. public static void StopBannerAutoRefresh(string adUnitIdentifier)
  276. {
  277. ValidateAdUnitIdentifier(adUnitIdentifier, "stop banner auto-refresh");
  278. _MaxStopBannerAutoRefresh(adUnitIdentifier);
  279. }
  280. [DllImport("__Internal")]
  281. private static extern void _MaxUpdateBannerPosition(string adUnitIdentifier, string bannerPosition);
  282. /// <summary>
  283. /// Updates the position of the banner to the new position provided.
  284. /// </summary>
  285. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to update the position. Must not be null.</param>
  286. /// <param name="bannerPosition">A new position for the banner. Must not be null.</param>
  287. public static void UpdateBannerPosition(string adUnitIdentifier, BannerPosition bannerPosition)
  288. {
  289. ValidateAdUnitIdentifier(adUnitIdentifier, "update banner position");
  290. _MaxUpdateBannerPosition(adUnitIdentifier, bannerPosition.ToSnakeCaseString());
  291. }
  292. [DllImport("__Internal")]
  293. private static extern void _MaxUpdateBannerPositionXY(string adUnitIdentifier, float x, float y);
  294. /// <summary>
  295. /// Updates the position of the banner to the new coordinates provided.
  296. /// </summary>
  297. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to update the position. Must not be null.</param>
  298. /// <param name="x">The X coordinate (horizontal position) of the banner relative to the top left corner of the screen.</param>
  299. /// <param name="y">The Y coordinate (vertical position) of the banner relative to the top left corner of the screen.</param>
  300. /// <seealso cref="GetBannerLayout">
  301. /// 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.
  302. /// </seealso>
  303. public static void UpdateBannerPosition(string adUnitIdentifier, float x, float y)
  304. {
  305. ValidateAdUnitIdentifier(adUnitIdentifier, "update banner position");
  306. _MaxUpdateBannerPositionXY(adUnitIdentifier, x, y);
  307. }
  308. [DllImport("__Internal")]
  309. private static extern void _MaxSetBannerWidth(string adUnitIdentifier, float width);
  310. /// <summary>
  311. /// Overrides the width of the banner in points.
  312. /// </summary>
  313. /// <param name="adUnitIdentifier">The ad unit identifier of the banner for which to override the width for. Must not be null.</param>
  314. /// <param name="width">The desired width of the banner in points</param>
  315. public static void SetBannerWidth(string adUnitIdentifier, float width)
  316. {
  317. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner width");
  318. _MaxSetBannerWidth(adUnitIdentifier, width);
  319. }
  320. [DllImport("__Internal")]
  321. private static extern void _MaxShowBanner(string adUnitIdentifier);
  322. /// <summary>
  323. /// Show banner at a position determined by the 'CreateBanner' call.
  324. /// </summary>
  325. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to show. Must not be null.</param>
  326. public static void ShowBanner(string adUnitIdentifier)
  327. {
  328. ValidateAdUnitIdentifier(adUnitIdentifier, "show banner");
  329. _MaxShowBanner(adUnitIdentifier);
  330. }
  331. [DllImport("__Internal")]
  332. private static extern void _MaxDestroyBanner(string adUnitIdentifier);
  333. /// <summary>
  334. /// Remove banner from the ad view and destroy it.
  335. /// </summary>
  336. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to destroy. Must not be null.</param>
  337. public static void DestroyBanner(string adUnitIdentifier)
  338. {
  339. ValidateAdUnitIdentifier(adUnitIdentifier, "destroy banner");
  340. _MaxDestroyBanner(adUnitIdentifier);
  341. }
  342. [DllImport("__Internal")]
  343. private static extern void _MaxHideBanner(string adUnitIdentifier);
  344. /// <summary>
  345. /// Hide banner.
  346. /// </summary>
  347. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to hide. Must not be null.</param>
  348. public static void HideBanner(string adUnitIdentifier)
  349. {
  350. ValidateAdUnitIdentifier(adUnitIdentifier, "hide banner");
  351. _MaxHideBanner(adUnitIdentifier);
  352. }
  353. [DllImport("__Internal")]
  354. private static extern void _MaxSetBannerBackgroundColor(string adUnitIdentifier, string hexColorCodeString);
  355. /// <summary>
  356. /// Set non-transparent background color for banners to be fully functional.
  357. /// </summary>
  358. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set background color for. Must not be null.</param>
  359. /// <param name="color">A background color to set for the ad</param>
  360. public static void SetBannerBackgroundColor(string adUnitIdentifier, Color color)
  361. {
  362. ValidateAdUnitIdentifier(adUnitIdentifier, "set background color");
  363. _MaxSetBannerBackgroundColor(adUnitIdentifier, MaxSdkUtils.ParseColor(color));
  364. }
  365. [DllImport("__Internal")]
  366. private static extern void _MaxSetBannerExtraParameter(string adUnitIdentifier, string key, string value);
  367. /// <summary>
  368. /// Set an extra parameter for the banner ad.
  369. /// </summary>
  370. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the extra parameter for. Must not be null.</param>
  371. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  372. /// <param name="value">The value for the extra parameter.</param>
  373. public static void SetBannerExtraParameter(string adUnitIdentifier, string key, string value)
  374. {
  375. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner extra parameter");
  376. _MaxSetBannerExtraParameter(adUnitIdentifier, key, value);
  377. }
  378. [DllImport("__Internal")]
  379. private static extern void _MaxSetBannerLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  380. [DllImport("__Internal")]
  381. private static extern void _MaxSetBannerLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  382. /// <summary>
  383. /// Set a local extra parameter for the banner ad.
  384. /// </summary>
  385. /// <param name="adUnitIdentifier">Ad unit identifier of the banner to set the local extra parameter for. Must not be null.</param>
  386. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  387. /// <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>
  388. public static void SetBannerLocalExtraParameter(string adUnitIdentifier, string key, object value)
  389. {
  390. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner local extra parameter");
  391. if (value == null || value is IntPtr)
  392. {
  393. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  394. _MaxSetBannerLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  395. }
  396. else
  397. {
  398. _MaxSetBannerLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  399. }
  400. }
  401. [DllImport("__Internal")]
  402. private static extern void _MaxSetBannerCustomData(string adUnitIdentifier, string customData);
  403. /// <summary>
  404. /// 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.
  405. /// </summary>
  406. /// <param name="adUnitIdentifier">Banner ad unit identifier of the banner to set the custom data for. Must not be null.</param>
  407. /// <param name="customData">The custom data to be set.</param>
  408. public static void SetBannerCustomData(string adUnitIdentifier, string customData)
  409. {
  410. ValidateAdUnitIdentifier(adUnitIdentifier, "set banner custom data");
  411. _MaxSetBannerCustomData(adUnitIdentifier, customData);
  412. }
  413. [DllImport("__Internal")]
  414. private static extern string _MaxGetBannerLayout(string adUnitIdentifier);
  415. /// <summary>
  416. /// 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)"/>,
  417. /// the banner is placed within the safe area of the screen. This returns the absolute position of the banner on screen.
  418. /// </summary>
  419. /// <param name="adUnitIdentifier">Ad unit identifier of the banner for which to get the position on screen. Must not be null.</param>
  420. /// <returns>A <see cref="Rect"/> representing the banner position on screen.</returns>
  421. public static Rect GetBannerLayout(string adUnitIdentifier)
  422. {
  423. ValidateAdUnitIdentifier(adUnitIdentifier, "get banner layout");
  424. var positionRect = _MaxGetBannerLayout(adUnitIdentifier);
  425. return GetRectFromString(positionRect);
  426. }
  427. #endregion
  428. #region MRECs
  429. [DllImport("__Internal")]
  430. private static extern void _MaxCreateMRec(string adUnitIdentifier, string mrecPosition);
  431. /// <summary>
  432. /// Create a new MREC.
  433. /// </summary>
  434. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to create. Must not be null.</param>
  435. /// <param name="mrecPosition">MREC position. Must not be null.</param>
  436. public static void CreateMRec(string adUnitIdentifier, AdViewPosition mrecPosition)
  437. {
  438. ValidateAdUnitIdentifier(adUnitIdentifier, "create MREC");
  439. _MaxCreateMRec(adUnitIdentifier, mrecPosition.ToSnakeCaseString());
  440. }
  441. [DllImport("__Internal")]
  442. private static extern void _MaxCreateMRecXY(string adUnitIdentifier, float x, float y);
  443. /// <summary>
  444. /// Create a new MREC with a custom position.
  445. /// </summary>
  446. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to create. Must not be null.</param>
  447. /// <param name="x">The X coordinate (horizontal position) of the MREC relative to the top left corner of the screen.</param>
  448. /// <param name="y">The Y coordinate (vertical position) of the MREC relative to the top left corner of the screen.</param>
  449. /// <seealso cref="GetMRecLayout">
  450. /// 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.
  451. /// </seealso>
  452. public static void CreateMRec(string adUnitIdentifier, float x, float y)
  453. {
  454. ValidateAdUnitIdentifier(adUnitIdentifier, "create MREC");
  455. _MaxCreateMRecXY(adUnitIdentifier, x, y);
  456. }
  457. [DllImport("__Internal")]
  458. private static extern void _MaxLoadMRec(string adUnitIdentifier);
  459. /// <summary>
  460. /// Load a new MREC ad.
  461. /// NOTE: The <see cref="CreateMRec()"/> method loads the first MREC ad and initiates an automated MREC refresh process.
  462. /// You only need to call this method if you pause MREC refresh.
  463. /// </summary>
  464. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to load. Must not be null.</param>
  465. public static void LoadMRec(string adUnitIdentifier)
  466. {
  467. ValidateAdUnitIdentifier(adUnitIdentifier, "load MREC");
  468. _MaxLoadMRec(adUnitIdentifier);
  469. }
  470. [DllImport("__Internal")]
  471. private static extern void _MaxSetMRecPlacement(string adUnitIdentifier, string placement);
  472. /// <summary>
  473. /// Set the MREC placement for an ad unit identifier to tie the future ad events to.
  474. /// </summary>
  475. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the placement for. Must not be null.</param>
  476. /// <param name="placement">Placement to set</param>
  477. public static void SetMRecPlacement(string adUnitIdentifier, string placement)
  478. {
  479. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC placement");
  480. _MaxSetMRecPlacement(adUnitIdentifier, placement);
  481. }
  482. [DllImport("__Internal")]
  483. private static extern void _MaxStartMRecAutoRefresh(string adUnitIdentifier);
  484. /// <summary>
  485. /// Starts or resumes auto-refreshing of the MREC for the given ad unit identifier.
  486. /// </summary>
  487. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to start auto-refresh. Must not be null.</param>
  488. public static void StartMRecAutoRefresh(string adUnitIdentifier)
  489. {
  490. ValidateAdUnitIdentifier(adUnitIdentifier, "start MREC auto-refresh");
  491. _MaxStartMRecAutoRefresh(adUnitIdentifier);
  492. }
  493. [DllImport("__Internal")]
  494. private static extern void _MaxStopMRecAutoRefresh(string adUnitIdentifeir);
  495. /// <summary>
  496. /// Pauses auto-refreshing of the MREC for the given ad unit identifier.
  497. /// </summary>
  498. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to stop auto-refresh. Must not be null.</param>
  499. public static void StopMRecAutoRefresh(string adUnitIdentifier)
  500. {
  501. ValidateAdUnitIdentifier(adUnitIdentifier, "stop MREC auto-refresh");
  502. _MaxStopMRecAutoRefresh(adUnitIdentifier);
  503. }
  504. [DllImport("__Internal")]
  505. private static extern void _MaxUpdateMRecPosition(string adUnitIdentifier, string mrecPosition);
  506. /// <summary>
  507. /// Updates the position of the MREC to the new position provided.
  508. /// </summary>
  509. /// <param name="adUnitIdentifier">The ad unit identifier of the MREC for which to update the position. Must not be null.</param>
  510. /// <param name="mrecPosition">A new position for the MREC. Must not be null.</param>
  511. public static void UpdateMRecPosition(string adUnitIdentifier, AdViewPosition mrecPosition)
  512. {
  513. ValidateAdUnitIdentifier(adUnitIdentifier, "update MREC position");
  514. _MaxUpdateMRecPosition(adUnitIdentifier, mrecPosition.ToSnakeCaseString());
  515. }
  516. [DllImport("__Internal")]
  517. private static extern void _MaxUpdateMRecPositionXY(string adUnitIdentifier, float x, float y);
  518. /// <summary>
  519. /// Updates the position of the MREC to the new coordinates provided.
  520. /// </summary>
  521. /// <param name="adUnitIdentifier">The ad unit identifier of the MREC for which to update the position. Must not be null.</param>
  522. /// <param name="x">The X coordinate (horizontal position) of the MREC relative to the top left corner of the screen.</param>
  523. /// <param name="y">The Y coordinate (vertical position) of the MREC relative to the top left corner of the screen.</param>
  524. /// <seealso cref="GetMRecLayout">
  525. /// 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.
  526. /// </seealso>
  527. public static void UpdateMRecPosition(string adUnitIdentifier, float x, float y)
  528. {
  529. ValidateAdUnitIdentifier(adUnitIdentifier, "update MREC position");
  530. _MaxUpdateMRecPositionXY(adUnitIdentifier, x, y);
  531. }
  532. [DllImport("__Internal")]
  533. private static extern void _MaxShowMRec(string adUnitIdentifier);
  534. /// <summary>
  535. /// Show MREC at a position determined by the 'CreateMRec' call.
  536. /// </summary>
  537. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to show. Must not be null.</param>
  538. public static void ShowMRec(string adUnitIdentifier)
  539. {
  540. ValidateAdUnitIdentifier(adUnitIdentifier, "show MREC");
  541. _MaxShowMRec(adUnitIdentifier);
  542. }
  543. [DllImport("__Internal")]
  544. private static extern void _MaxDestroyMRec(string adUnitIdentifier);
  545. /// <summary>
  546. /// Remove MREC from the ad view and destroy it.
  547. /// </summary>
  548. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to destroy. Must not be null.</param>
  549. public static void DestroyMRec(string adUnitIdentifier)
  550. {
  551. ValidateAdUnitIdentifier(adUnitIdentifier, "destroy MREC");
  552. _MaxDestroyMRec(adUnitIdentifier);
  553. }
  554. [DllImport("__Internal")]
  555. private static extern void _MaxHideMRec(string adUnitIdentifier);
  556. /// <summary>
  557. /// Hide MREC.
  558. /// </summary>
  559. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to hide. Must not be null.</param>
  560. public static void HideMRec(string adUnitIdentifier)
  561. {
  562. ValidateAdUnitIdentifier(adUnitIdentifier, "hide MREC");
  563. _MaxHideMRec(adUnitIdentifier);
  564. }
  565. [DllImport("__Internal")]
  566. private static extern void _MaxSetMRecExtraParameter(string adUnitIdentifier, string key, string value);
  567. /// <summary>
  568. /// Set an extra parameter for the MREC ad.
  569. /// </summary>
  570. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the extra parameter for. Must not be null.</param>
  571. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  572. /// <param name="value">The value for the extra parameter.</param>
  573. public static void SetMRecExtraParameter(string adUnitIdentifier, string key, string value)
  574. {
  575. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC extra parameter");
  576. _MaxSetMRecExtraParameter(adUnitIdentifier, key, value);
  577. }
  578. [DllImport("__Internal")]
  579. private static extern void _MaxSetMRecLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  580. [DllImport("__Internal")]
  581. private static extern void _MaxSetMRecLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  582. /// <summary>
  583. /// Set a local extra parameter for the MREC ad.
  584. /// </summary>
  585. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC to set the local extra parameter for. Must not be null.</param>
  586. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  587. /// <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>
  588. public static void SetMRecLocalExtraParameter(string adUnitIdentifier, string key, object value)
  589. {
  590. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC local extra parameter");
  591. if (value == null || value is IntPtr)
  592. {
  593. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  594. _MaxSetMRecLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  595. }
  596. else
  597. {
  598. _MaxSetMRecLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  599. }
  600. }
  601. [DllImport("__Internal")]
  602. private static extern void _MaxSetMRecCustomData(string adUnitIdentifier, string value);
  603. /// <summary>
  604. /// 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.
  605. /// </summary>
  606. /// <param name="adUnitIdentifier">MREC Ad unit identifier of the banner to set the custom data for. Must not be null.</param>
  607. /// <param name="customData">The custom data to be set.</param>
  608. public static void SetMRecCustomData(string adUnitIdentifier, string customData)
  609. {
  610. ValidateAdUnitIdentifier(adUnitIdentifier, "set MREC custom data");
  611. _MaxSetMRecCustomData(adUnitIdentifier, customData);
  612. }
  613. [DllImport("__Internal")]
  614. private static extern string _MaxGetMRecLayout(string adUnitIdentifier);
  615. /// <summary>
  616. /// 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)"/>,
  617. /// the banner is placed within the safe area of the screen. This returns the absolute position of the MREC on screen.
  618. /// </summary>
  619. /// <param name="adUnitIdentifier">Ad unit identifier of the MREC for which to get the position on screen. Must not be null.</param>
  620. /// <returns>A <see cref="Rect"/> representing the banner position on screen.</returns>
  621. public static Rect GetMRecLayout(string adUnitIdentifier)
  622. {
  623. ValidateAdUnitIdentifier(adUnitIdentifier, "get MREC layout");
  624. var positionRect = _MaxGetMRecLayout(adUnitIdentifier);
  625. return GetRectFromString(positionRect);
  626. }
  627. #endregion
  628. #region Interstitials
  629. [DllImport("__Internal")]
  630. private static extern void _MaxLoadInterstitial(string adUnitIdentifier);
  631. /// <summary>
  632. /// Start loading an interstitial.
  633. /// </summary>
  634. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load. Must not be null.</param>
  635. public static void LoadInterstitial(string adUnitIdentifier)
  636. {
  637. ValidateAdUnitIdentifier(adUnitIdentifier, "load interstitial");
  638. _MaxLoadInterstitial(adUnitIdentifier);
  639. }
  640. [DllImport("__Internal")]
  641. private static extern bool _MaxIsInterstitialReady(string adUnitIdentifier);
  642. /// <summary>
  643. /// Check if interstitial ad is loaded and ready to be displayed.
  644. /// </summary>
  645. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial ad to check if it's ready to be displayed. Must not be null.</param>
  646. /// <returns>True if the ad is ready to be displayed</returns>
  647. public static bool IsInterstitialReady(string adUnitIdentifier)
  648. {
  649. ValidateAdUnitIdentifier(adUnitIdentifier, "check interstitial loaded");
  650. return _MaxIsInterstitialReady(adUnitIdentifier);
  651. }
  652. [DllImport("__Internal")]
  653. private static extern void _MaxShowInterstitial(string adUnitIdentifier, string placement, string customData);
  654. /// <summary>
  655. /// 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.
  656. /// </summary>
  657. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load. Must not be null.</param>
  658. /// <param name="placement">The placement to tie the showing ad's events to</param>
  659. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  660. public static void ShowInterstitial(string adUnitIdentifier, string placement = null, string customData = null)
  661. {
  662. ValidateAdUnitIdentifier(adUnitIdentifier, "show interstitial");
  663. if (IsInterstitialReady(adUnitIdentifier))
  664. {
  665. _MaxShowInterstitial(adUnitIdentifier, placement, customData);
  666. }
  667. else
  668. {
  669. MaxSdkLogger.UserWarning("Not showing MAX Ads interstitial: ad not ready");
  670. }
  671. }
  672. [DllImport("__Internal")]
  673. private static extern void _MaxSetInterstitialExtraParameter(string adUnitIdentifier, string key, string value);
  674. /// <summary>
  675. /// Set an extra parameter for the ad.
  676. /// </summary>
  677. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to set the extra parameter for. Must not be null.</param>
  678. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  679. /// <param name="value">The value for the extra parameter.</param>
  680. public static void SetInterstitialExtraParameter(string adUnitIdentifier, string key, string value)
  681. {
  682. ValidateAdUnitIdentifier(adUnitIdentifier, "set interstitial extra parameter");
  683. _MaxSetInterstitialExtraParameter(adUnitIdentifier, key, value);
  684. }
  685. [DllImport("__Internal")]
  686. private static extern void _MaxSetInterstitialLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  687. [DllImport("__Internal")]
  688. private static extern void _MaxSetInterstitialLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  689. /// <summary>
  690. /// Set a local extra parameter for the ad.
  691. /// </summary>
  692. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to set the local extra parameter for. Must not be null.</param>
  693. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  694. /// <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>
  695. public static void SetInterstitialLocalExtraParameter(string adUnitIdentifier, string key, object value)
  696. {
  697. ValidateAdUnitIdentifier(adUnitIdentifier, "set interstitial local extra parameter");
  698. if (value == null || value is IntPtr)
  699. {
  700. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  701. _MaxSetInterstitialLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  702. }
  703. else
  704. {
  705. _MaxSetInterstitialLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  706. }
  707. }
  708. #endregion
  709. #region App Open Ads
  710. [DllImport("__Internal")]
  711. private static extern void _MaxLoadAppOpenAd(string adUnitIdentifier);
  712. /// <summary>
  713. /// Start loading an app open ad.
  714. /// </summary>
  715. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  716. public static void LoadAppOpenAd(string adUnitIdentifier)
  717. {
  718. ValidateAdUnitIdentifier(adUnitIdentifier, "load app open ad");
  719. _MaxLoadAppOpenAd(adUnitIdentifier);
  720. }
  721. [DllImport("__Internal")]
  722. private static extern bool _MaxIsAppOpenAdReady(string adUnitIdentifier);
  723. /// <summary>
  724. /// Check if app open ad ad is loaded and ready to be displayed.
  725. /// </summary>
  726. /// <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>
  727. /// <returns>True if the ad is ready to be displayed</returns>
  728. public static bool IsAppOpenAdReady(string adUnitIdentifier)
  729. {
  730. ValidateAdUnitIdentifier(adUnitIdentifier, "check app open ad loaded");
  731. return _MaxIsAppOpenAdReady(adUnitIdentifier);
  732. }
  733. [DllImport("__Internal")]
  734. private static extern void _MaxShowAppOpenAd(string adUnitIdentifier, string placement, string customData);
  735. /// <summary>
  736. /// 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.
  737. /// </summary>
  738. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to load. Must not be null.</param>
  739. /// <param name="placement">The placement to tie the showing ad's events to</param>
  740. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  741. public static void ShowAppOpenAd(string adUnitIdentifier, string placement = null, string customData = null)
  742. {
  743. ValidateAdUnitIdentifier(adUnitIdentifier, "show app open ad");
  744. if (IsAppOpenAdReady(adUnitIdentifier))
  745. {
  746. _MaxShowAppOpenAd(adUnitIdentifier, placement, customData);
  747. }
  748. else
  749. {
  750. MaxSdkLogger.UserWarning("Not showing MAX Ads app open ad: ad not ready");
  751. }
  752. }
  753. [DllImport("__Internal")]
  754. private static extern void _MaxSetAppOpenAdExtraParameter(string adUnitIdentifier, string key, string value);
  755. /// <summary>
  756. /// Set an extra parameter for the ad.
  757. /// </summary>
  758. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to set the extra parameter for. Must not be null.</param>
  759. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  760. /// <param name="value">The value for the extra parameter.</param>
  761. public static void SetAppOpenAdExtraParameter(string adUnitIdentifier, string key, string value)
  762. {
  763. ValidateAdUnitIdentifier(adUnitIdentifier, "set app open ad extra parameter");
  764. _MaxSetAppOpenAdExtraParameter(adUnitIdentifier, key, value);
  765. }
  766. [DllImport("__Internal")]
  767. private static extern void _MaxSetAppOpenAdLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  768. [DllImport("__Internal")]
  769. private static extern void _MaxSetAppOpenAdLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  770. /// <summary>
  771. /// Set a local extra parameter for the ad.
  772. /// </summary>
  773. /// <param name="adUnitIdentifier">Ad unit identifier of the app open ad to set the local extra parameter for. Must not be null.</param>
  774. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  775. /// <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>
  776. public static void SetAppOpenAdLocalExtraParameter(string adUnitIdentifier, string key, object value)
  777. {
  778. ValidateAdUnitIdentifier(adUnitIdentifier, "set app open ad local extra parameter");
  779. if (value == null || value is IntPtr)
  780. {
  781. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  782. _MaxSetAppOpenAdLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  783. }
  784. else
  785. {
  786. _MaxSetAppOpenAdLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  787. }
  788. }
  789. #endregion
  790. #region Rewarded
  791. [DllImport("__Internal")]
  792. private static extern void _MaxLoadRewardedAd(string adUnitIdentifier);
  793. /// <summary>
  794. /// Start loading an rewarded ad.
  795. /// </summary>
  796. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to load. Must not be null.</param>
  797. public static void LoadRewardedAd(string adUnitIdentifier)
  798. {
  799. ValidateAdUnitIdentifier(adUnitIdentifier, "load rewarded ad");
  800. _MaxLoadRewardedAd(adUnitIdentifier);
  801. }
  802. [DllImport("__Internal")]
  803. private static extern bool _MaxIsRewardedAdReady(string adUnitIdentifier);
  804. /// <summary>
  805. /// Check if rewarded ad ad is loaded and ready to be displayed.
  806. /// </summary>
  807. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to check if it's ready to be displayed. Must not be null.</param>
  808. /// <returns>True if the ad is ready to be displayed</returns>
  809. public static bool IsRewardedAdReady(string adUnitIdentifier)
  810. {
  811. ValidateAdUnitIdentifier(adUnitIdentifier, "check rewarded ad loaded");
  812. return _MaxIsRewardedAdReady(adUnitIdentifier);
  813. }
  814. [DllImport("__Internal")]
  815. private static extern void _MaxShowRewardedAd(string adUnitIdentifier, string placement, string customData);
  816. /// <summary>
  817. /// 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.
  818. /// </summary>
  819. /// <param name="adUnitIdentifier">Ad unit identifier of the interstitial to load. Must not be null.</param>
  820. /// <param name="placement">The placement to tie the showing ad's events to</param>
  821. /// <param name="customData">The custom data to tie the showing ad's events to. Maximum size is 8KB.</param>
  822. public static void ShowRewardedAd(string adUnitIdentifier, string placement = null, string customData = null)
  823. {
  824. ValidateAdUnitIdentifier(adUnitIdentifier, "show rewarded ad");
  825. if (IsRewardedAdReady(adUnitIdentifier))
  826. {
  827. _MaxShowRewardedAd(adUnitIdentifier, placement, customData);
  828. }
  829. else
  830. {
  831. MaxSdkLogger.UserWarning("Not showing MAX Ads rewarded ad: ad not ready");
  832. }
  833. }
  834. [DllImport("__Internal")]
  835. private static extern void _MaxSetRewardedAdExtraParameter(string adUnitIdentifier, string key, string value);
  836. /// <summary>
  837. /// Set an extra parameter for the ad.
  838. /// </summary>
  839. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to set the extra parameter for. Must not be null.</param>
  840. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  841. /// <param name="value">The value for the extra parameter.</param>
  842. public static void SetRewardedAdExtraParameter(string adUnitIdentifier, string key, string value)
  843. {
  844. ValidateAdUnitIdentifier(adUnitIdentifier, "set rewarded extra parameter");
  845. _MaxSetRewardedAdExtraParameter(adUnitIdentifier, key, value);
  846. }
  847. [DllImport("__Internal")]
  848. private static extern void _MaxSetRewardedAdLocalExtraParameter(string adUnitIdentifier, string key, IntPtr value);
  849. [DllImport("__Internal")]
  850. private static extern void _MaxSetRewardedAdLocalExtraParameterJSON(string adUnitIdentifier, string key, string json);
  851. /// <summary>
  852. /// Set a local extra parameter for the ad.
  853. /// </summary>
  854. /// <param name="adUnitIdentifier">Ad unit identifier of the rewarded ad to set the local extra parameter for. Must not be null.</param>
  855. /// <param name="key">The key for the local extra parameter. Must not be null.</param>
  856. /// <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>
  857. public static void SetRewardedAdLocalExtraParameter(string adUnitIdentifier, string key, object value)
  858. {
  859. ValidateAdUnitIdentifier(adUnitIdentifier, "set rewarded ad local extra parameter");
  860. if (value == null || value is IntPtr)
  861. {
  862. var intPtrValue = value == null ? IntPtr.Zero : (IntPtr) value;
  863. _MaxSetRewardedAdLocalExtraParameter(adUnitIdentifier, key, intPtrValue);
  864. }
  865. else
  866. {
  867. _MaxSetRewardedAdLocalExtraParameterJSON(adUnitIdentifier, key, SerializeLocalExtraParameterValue(value));
  868. }
  869. }
  870. #endregion
  871. #region Event Tracking
  872. [DllImport("__Internal")]
  873. private static extern void _MaxTrackEvent(string name, string parameters);
  874. /// <summary>
  875. /// Track an event using AppLovin.
  876. /// </summary>
  877. /// <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>
  878. /// <param name="parameters">A dictionary containing key-value pairs further describing this event.</param>
  879. public static void TrackEvent(string name, IDictionary<string, string> parameters = null)
  880. {
  881. _MaxTrackEvent(name, Json.Serialize(parameters));
  882. }
  883. #endregion
  884. #region Settings
  885. [DllImport("__Internal")]
  886. private static extern void _MaxSetMuted(bool muted);
  887. /// <summary>
  888. /// Set whether to begin video ads in a muted state or not.
  889. ///
  890. /// Please call this method after the SDK has initialized.
  891. /// </summary>
  892. /// <param name="muted"><c>true</c> if video ads should being in muted state.</param>
  893. public static void SetMuted(bool muted)
  894. {
  895. _MaxSetMuted(muted);
  896. }
  897. [DllImport("__Internal")]
  898. private static extern bool _MaxIsMuted();
  899. /// <summary>
  900. /// Whether video ads begin in a muted state or not. Defaults to <c>false</c>.
  901. ///
  902. /// Note: Returns <c>false</c> if the SDK is not initialized.
  903. /// </summary>
  904. /// <returns><c>true</c> if video ads begin in muted state.</returns>
  905. public static bool IsMuted()
  906. {
  907. return _MaxIsMuted();
  908. }
  909. [DllImport("__Internal")]
  910. private static extern bool _MaxSetVerboseLogging(bool enabled);
  911. /// <summary>
  912. /// 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.
  913. /// </summary>
  914. /// <param name="enabled"><c>true</c> if verbose logging should be enabled.</param>
  915. public static void SetVerboseLogging(bool enabled)
  916. {
  917. _MaxSetVerboseLogging(enabled);
  918. }
  919. [DllImport("__Internal")]
  920. private static extern bool _MaxIsVerboseLoggingEnabled();
  921. /// <summary>
  922. /// Whether or not verbose logging is enabled.
  923. /// </summary>
  924. /// <returns><c>true</c> if verbose logging is enabled.</returns>
  925. public static bool IsVerboseLoggingEnabled()
  926. {
  927. return _MaxIsVerboseLoggingEnabled();
  928. }
  929. [DllImport("__Internal")]
  930. private static extern bool _MaxSetCreativeDebuggerEnabled(bool enabled);
  931. /// <summary>
  932. /// Whether the creative debugger will be displayed on fullscreen ads after flipping the device screen down twice. Defaults to true.
  933. /// </summary>
  934. /// <param name="enabled"><c>true</c> if the creative debugger should be enabled.</param>
  935. public static void SetCreativeDebuggerEnabled(bool enabled)
  936. {
  937. _MaxSetCreativeDebuggerEnabled(enabled);
  938. }
  939. [DllImport("__Internal")]
  940. private static extern void _MaxSetTestDeviceAdvertisingIdentifiers(string[] advertisingIdentifiers, int size);
  941. /// <summary>
  942. /// Enable devices to receive test ads, by passing in the advertising identifier (IDFA/GAID) of each test device.
  943. /// Refer to AppLovin logs for the IDFA/GAID of your current device.
  944. /// </summary>
  945. /// <param name="advertisingIdentifiers">String list of advertising identifiers from devices to receive test ads.</param>
  946. public static void SetTestDeviceAdvertisingIdentifiers(string[] advertisingIdentifiers)
  947. {
  948. if (IsInitialized())
  949. {
  950. MaxSdkLogger.UserError("Test Device Advertising Identifiers must be set before SDK initialization.");
  951. return;
  952. }
  953. _MaxSetTestDeviceAdvertisingIdentifiers(advertisingIdentifiers, advertisingIdentifiers.Length);
  954. }
  955. [DllImport("__Internal")]
  956. private static extern bool _MaxSetExceptionHandlerEnabled(bool enabled);
  957. /// <summary>
  958. /// Whether or not the native AppLovin SDKs listen to exceptions. Defaults to <c>true</c>.
  959. /// </summary>
  960. /// <param name="enabled"><c>true</c> if the native AppLovin SDKs should not listen to exceptions.</param>
  961. public static void SetExceptionHandlerEnabled(bool enabled)
  962. {
  963. _MaxSetExceptionHandlerEnabled(enabled);
  964. }
  965. [DllImport("__Internal")]
  966. private static extern void _MaxSetExtraParameter(string key, string value);
  967. /// <summary>
  968. /// Set an extra parameter to pass to the AppLovin server.
  969. /// </summary>
  970. /// <param name="key">The key for the extra parameter. Must not be null.</param>
  971. /// <param name="value">The value for the extra parameter. May be null.</param>
  972. public static void SetExtraParameter(string key, string value)
  973. {
  974. HandleExtraParameter(key, value);
  975. _MaxSetExtraParameter(key, value);
  976. }
  977. [DllImport("__Internal")]
  978. private static extern IntPtr _MaxGetSafeAreaInsets();
  979. /// <summary>
  980. /// Get the native insets in pixels for the safe area.
  981. /// These insets are used to position ads within the safe area of the screen.
  982. /// </summary>
  983. public static SafeAreaInsets GetSafeAreaInsets()
  984. {
  985. // Use an int array instead of json serialization for performance
  986. var insetsPtr = _MaxGetSafeAreaInsets();
  987. var insets = new int[4];
  988. Marshal.Copy(insetsPtr, insets, 0, 4);
  989. // Convert from points to pixels
  990. var screenDensity = MaxSdkUtils.GetScreenDensity();
  991. for (var i = 0; i < insets.Length; i++)
  992. {
  993. insets[i] *= (int) screenDensity;
  994. }
  995. return new SafeAreaInsets(insets);
  996. }
  997. #endregion
  998. #region Private
  999. [MonoPInvokeCallback(typeof(ALUnityBackgroundCallback))]
  1000. internal static void BackgroundCallback(string propsStr)
  1001. {
  1002. HandleBackgroundCallback(propsStr);
  1003. }
  1004. #endregion
  1005. #region Obsolete
  1006. [DllImport("__Internal")]
  1007. private static extern void _MaxSetSdkKey(string sdkKey);
  1008. [Obsolete("This API has been deprecated and will be removed in a future release. Please set your SDK key in the AppLovin Integration Manager.")]
  1009. public static void SetSdkKey(string sdkKey)
  1010. {
  1011. _MaxSetSdkKey(sdkKey);
  1012. 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.");
  1013. }
  1014. #endregion
  1015. #endif
  1016. }