1
0

Advertisement.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using System;
  2. using UnityEngine.Advertisements.Platform;
  3. using UnityEngine.Advertisements.Utilities;
  4. namespace UnityEngine.Advertisements
  5. {
  6. /// <summary>
  7. /// The wrapper class used to interact with the Unity Ads SDK.
  8. /// </summary>
  9. public static class Advertisement
  10. {
  11. private static IPlatform s_Platform;
  12. static Advertisement()
  13. {
  14. if (s_Platform == null)
  15. {
  16. s_Platform = CreatePlatform();
  17. }
  18. }
  19. /// <summary>
  20. /// Returns <c>true</c> if the SDK is initialized successfully, and <c>false</c> if it isn't.
  21. /// </summary>
  22. public static bool isInitialized => s_Platform.IsInitialized;
  23. /// <summary>
  24. /// Returns <c>true</c> if the SDK is supported on the current platform, and <c>false</c> if it isn't.
  25. /// </summary>
  26. public static bool isSupported => IsSupported();
  27. /// <summary>
  28. /// Returns <c>true</c> if the SDK is is in debug mode, and <c>false</c> if it isn't. Debug mode controls the level of logging from the SDK.
  29. /// </summary>
  30. public static bool debugMode
  31. {
  32. get => s_Platform.DebugMode;
  33. set => s_Platform.DebugMode = value;
  34. }
  35. /// <summary>
  36. /// Returns the current SDK version.
  37. /// </summary>
  38. public static string version => s_Platform.Version;
  39. /// <summary>
  40. /// Returns <c>true</c> if an ad is currently showing, and <c>false</c> if it isn't.
  41. /// </summary>
  42. public static bool isShowing => s_Platform.IsShowing;
  43. /// <summary>
  44. /// Initializes the SDK with a specified <a href="../manual/MonetizationResourcesDashboardGuide.html#project-settings">Game ID</a>.
  45. /// </summary>
  46. /// <param name="gameId">The platform-specific Unity game identifier for your Project, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  47. public static void Initialize(string gameId)
  48. {
  49. Initialize(gameId, false, false);
  50. }
  51. /// <summary>
  52. /// Initializes the SDK with a specified Game ID and test mode setting.
  53. /// </summary>
  54. /// <param name="gameId">The platform-specific Unity game identifier for your Project, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  55. /// <param name="testMode"><a href="../manual/MonetizationResourcesDashboardGuide.html#project-settings">Test mode</a> allows you to test your integration without serving live ads. Use <c>true</c> to initialize in test mode.</param>
  56. public static void Initialize(string gameId, bool testMode)
  57. {
  58. Initialize(gameId, testMode, false);
  59. }
  60. /// <summary>
  61. /// Initializes the SDK with a specified Game ID and test mode setting.
  62. /// </summary>
  63. /// <param name="gameId">The platform-specific Unity game identifier for your Project, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  64. /// <param name="testMode"><a href="../manual/MonetizationResourcesDashboardGuide.html#project-settings">Test mode</a> allows you to test your integration without serving live ads. Use <c>true</c> to initialize in test mode.</param>
  65. /// <param name="enablePerPlacementLoad">Enable the load API lifecycle. See <see cref="Advertisements.Advertisement.Load(string)"/> for more information.</param>
  66. public static void Initialize(string gameId, bool testMode, bool enablePerPlacementLoad)
  67. {
  68. Initialize(gameId, testMode, enablePerPlacementLoad, null);
  69. }
  70. /// <summary>
  71. /// Initializes the SDK with a specified Game ID, test mode setting, and Placement load setting.
  72. /// </summary>
  73. /// <param name="gameId">The platform-specific Unity <a href="../manual/MonetizationResourcesDashboardGuide.html#project-settings">game identifier</a> for your Project, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  74. /// <param name="testMode"><a href="../manual/MonetizationResourcesDashboardGuide.html#project-settings">Test mode</a> allows you to test your integration without serving live ads. Use <c>true</c> to initialize in test mode.</param>
  75. /// <param name="enablePerPlacementLoad">Enable the load API lifecycle. See <see cref="Advertisements.Advertisement.Load(string)"/> for more information.</param>
  76. /// <param name="initializationListener">Listener for IUnityAdsInitializationListener callbacks</param>
  77. public static void Initialize(string gameId, bool testMode, bool enablePerPlacementLoad, IUnityAdsInitializationListener initializationListener)
  78. {
  79. #if UNITY_2020_1_OR_NEWER && ENABLE_CLOUD_SERVICES_ADS && (UNITY_IOS || UNITY_ANDROID || UNITY_EDITOR)
  80. if (testMode == false && UnityAdsSettings.testMode)
  81. {
  82. Debug.Log("Unity Ads is initializing in test mode since test mode is enabled in Service Window.");
  83. }
  84. s_Platform.Initialize(gameId, UnityAdsSettings.testMode || testMode, enablePerPlacementLoad, initializationListener);
  85. #else
  86. s_Platform.Initialize(gameId, testMode, enablePerPlacementLoad, initializationListener);
  87. #endif
  88. }
  89. /// <summary>
  90. /// Returns <c>true</c> if an ad is available to display on the default <a href="../manual/MonetizationPlacements.html">Placement</a>, and <c>false</c> if it isn't.
  91. /// </summary>
  92. public static bool IsReady()
  93. {
  94. return s_Platform.IsReady(null);
  95. }
  96. /// <summary>
  97. /// Returns <c>true</c> if an ad is available to display for a specified <a href="../manual/MonetizationPlacements.html">Placement</a>, and <c>false</c> if it isn't.
  98. /// </summary>
  99. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  100. public static bool IsReady(string placementId)
  101. {
  102. return s_Platform.IsReady(placementId);
  103. }
  104. /// <summary>
  105. /// Loads ad content for a specified Placement. If you initialized the SDK with <c>enablePerPlacementLoad</c> enabled, you must call <c>Load</c> before calling <c>Show</c>. Note that the <c>Load</c> API is in beta and available upon invite only. If you would like to be considered for the beta, please contact us at unityads-support@unity3d.com.
  106. /// </summary>
  107. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  108. /// <seealso cref="Advertisement.Initialize(string, bool, bool)"/>
  109. /// <seealso cref="Advertisement.Show(string)"/>
  110. public static void Load(string placementId)
  111. {
  112. Load(placementId, null);
  113. }
  114. /// <summary>
  115. /// Loads ad content for a specified Placement. If you initialized the SDK with <c>enablePerPlacementLoad</c> enabled, you must call <c>Load</c> before calling <c>Show</c>. Note that the <c>Load</c> API is in beta and available upon invite only. If you would like to be considered for the beta, please contact us at unityads-support@unity3d.com.
  116. /// </summary>
  117. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  118. /// <param name="loadListener">A listener for <c>IUnityAdsLoadListener</c> callbacks</param>
  119. /// <seealso cref="Advertisement.Initialize(string, bool, bool)"/>
  120. /// <seealso cref="Advertisement.Show(string)"/>
  121. public static void Load(string placementId, IUnityAdsLoadListener loadListener)
  122. {
  123. s_Platform.Load(placementId, loadListener);
  124. }
  125. /// <summary>
  126. /// Displays an ad in the <a href="https://unityads.unity3d.com/help/monetization/ad-units-management#unity-standard-placements">default Ad Unit or Placement</a> if it is ready.
  127. /// </summary>
  128. /// <seealso cref="Advertisement.IsReady()"/>
  129. public static void Show()
  130. {
  131. s_Platform.Show();
  132. }
  133. /// <summary>
  134. /// Displays an ad in the default <a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready, and passes a <a href="..api/UnityEngine.Advertisements.ShowResult.html"><c>ShowResult</c></a> enum to the <a href="../api/UnityEngine.Advertisements.ShowOptions.html"><c>ShowOptions.resultCallback</c></a> callback when the ad finishes.
  135. /// </summary>
  136. /// <param name="showOptions">A collection of options, including <c>resultCallback</c>, for modifying ad behavior.</param>
  137. public static void Show(ShowOptions showOptions)
  138. {
  139. Show(null, showOptions, null);
  140. }
  141. /// <summary>
  142. /// Displays an ad in a specified <a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready.
  143. /// </summary>
  144. /// <seealso cref="Advertisement.IsReady()"/>
  145. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  146. public static void Show(string placementId)
  147. {
  148. Show(placementId, null, null);
  149. }
  150. /// <summary>
  151. /// Displays an ad in a specified <a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready, and passes a <a href="..api/UnityEngine.Advertisements.ShowResult.html"><c>ShowResult</c></a> enum to the <a href="../api/UnityEngine.Advertisements.ShowOptions.html"><c>ShowOptions.resultCallback</c></a> callback when the ad finishes.
  152. /// </summary>
  153. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  154. /// <param name="showOptions">A collection of options, including <c>resultCallback</c>, for modifying ad behavior.</param>
  155. public static void Show(string placementId, ShowOptions showOptions)
  156. {
  157. Show(placementId, showOptions, null);
  158. }
  159. /// <summary>
  160. /// Displays an ad in a specified <a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready.
  161. /// </summary>
  162. /// <seealso cref="Advertisement.IsReady()"/>
  163. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  164. /// <param name="showListener">A listener for <c>IUnityAdsShowListener</c> callbacks</param>
  165. public static void Show(string placementId, IUnityAdsShowListener showListener)
  166. {
  167. Show(placementId, null, showListener);
  168. }
  169. /// <summary>
  170. /// Displays an ad in a specified <a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready, and passes a <a href="..api/UnityEngine.Advertisements.ShowResult.html"><c>ShowResult</c></a> enum to the <a href="../api/UnityEngine.Advertisements.ShowOptions.html"><c>ShowOptions.resultCallback</c></a> callback when the ad finishes.
  171. /// </summary>
  172. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  173. /// <param name="showOptions">A collection of options, including <c>resultCallback</c>, for modifying ad behavior.</param>
  174. /// <param name="showListener">A listener for <c>IUnityAdsShowListener</c> callbacks</param>
  175. public static void Show(string placementId, ShowOptions showOptions, IUnityAdsShowListener showListener)
  176. {
  177. s_Platform.Show(placementId, showOptions, showListener);
  178. }
  179. /// <summary>
  180. /// Sets various metadata for the SDK.
  181. /// </summary>
  182. /// <param name="metaData">A metadata container.</param>
  183. public static void SetMetaData(MetaData metaData)
  184. {
  185. s_Platform.SetMetaData(metaData);
  186. }
  187. /// <summary>
  188. /// Adds a listener that will recieve Unity Ads callbacks. SDK versions 3.1+ allow you to register multiple listeners. This is especially helpful for mediation customers.
  189. /// </summary>
  190. /// <param name="listener">A listener for Unity Ads callbacks.</param>
  191. public static void AddListener(IUnityAdsListener listener)
  192. {
  193. s_Platform?.AddListener(listener);
  194. }
  195. /// <summary>
  196. /// Allows you to remove an active listener.
  197. /// </summary>
  198. /// <param name="listener">A listener for Unity Ads callbacks.</param>
  199. public static void RemoveListener(IUnityAdsListener listener)
  200. {
  201. s_Platform?.RemoveListener(listener);
  202. }
  203. /// <summary>
  204. /// Returns the state of the default <a href="../manual/MonetizationPlacements.html">Placement</a>.
  205. /// </summary>
  206. public static PlacementState GetPlacementState()
  207. {
  208. return s_Platform.GetPlacementState(null);
  209. }
  210. /// <summary>
  211. /// Returns the state of a specified <a href="../manual/MonetizationPlacements.html">Placement</a>.
  212. /// </summary>
  213. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  214. public static PlacementState GetPlacementState(string placementId)
  215. {
  216. return s_Platform.GetPlacementState(string.IsNullOrEmpty(placementId) ? null : placementId);
  217. }
  218. private static IPlatform CreatePlatform()
  219. {
  220. try
  221. {
  222. IUnityLifecycleManager unityLifecycleManager = new UnityLifecycleManager();
  223. INativePlatform nativePlatform;
  224. INativeBanner nativeBanner;
  225. #if UNITY_EDITOR
  226. nativeBanner = new Platform.Editor.EditorBanner();
  227. nativePlatform = new Platform.Editor.EditorPlatform();
  228. #elif UNITY_ANDROID
  229. nativeBanner = new Platform.Android.AndroidBanner();
  230. var androidPlatform = new Platform.Android.AndroidPlatform();
  231. unityLifecycleManager.SetOnApplicationQuitCallback(() => {
  232. androidPlatform.RemoveListener();
  233. });
  234. nativePlatform = androidPlatform;
  235. #elif UNITY_IOS
  236. nativeBanner = new Platform.iOS.IosBanner();
  237. nativePlatform = new Platform.iOS.IosPlatform();
  238. #else
  239. nativeBanner = new Platform.Unsupported.UnsupportedBanner();
  240. nativePlatform = new Platform.Unsupported.UnsupportedPlatform();
  241. #endif
  242. IBanner banner = new Advertisements.Banner(nativeBanner, unityLifecycleManager);
  243. return new Platform.Platform(nativePlatform, banner, unityLifecycleManager);
  244. }
  245. catch (Exception exception)
  246. {
  247. try
  248. {
  249. Debug.LogError("Initializing Unity Ads.");
  250. Debug.LogError(exception.Message);
  251. }
  252. catch (MissingMethodException)
  253. {
  254. }
  255. var unsupportedPlatform = new Platform.Unsupported.UnsupportedPlatform();
  256. var coroutineExecutor = new UnityLifecycleManager();
  257. var unsupportedBanner = new Platform.Unsupported.UnsupportedBanner();
  258. var genericBanner = new Advertisements.Banner(unsupportedBanner, coroutineExecutor);
  259. return new Platform.Platform(unsupportedPlatform, genericBanner, coroutineExecutor);
  260. }
  261. }
  262. private static bool IsSupported()
  263. {
  264. return Application.isEditor || Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer;
  265. }
  266. /// <summary>
  267. /// A static class for implementing banner ads.
  268. /// </summary>
  269. public static class Banner
  270. {
  271. /// <summary>
  272. /// Loads the banner ad with the default <a href="../manual/MonetizationPlacements.html">Placement</a>, and no callbacks.
  273. /// </summary>
  274. public static void Load()
  275. {
  276. Load(null, null);
  277. }
  278. /// <summary>
  279. /// Loads the banner ad with the default <a href="../manual/MonetizationPlacements.html">Placement</a>, but fires the <c>loadCallback</c> callback on successful load, and the <c>errorCallback</c> callback on failure to load.
  280. /// </summary>
  281. /// <param name="options">A collection of options that notify the SDK of events when loading the banner.</param>
  282. public static void Load(BannerLoadOptions options)
  283. {
  284. Load(null, options);
  285. }
  286. /// <summary>
  287. /// Loads the banner ad with a specified <a href="../manual/MonetizationPlacements.html">Placement</a>, and no callbacks.
  288. /// </summary>
  289. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  290. public static void Load(string placementId)
  291. {
  292. Load(placementId, null);
  293. }
  294. /// <summary>
  295. /// Loads the banner ad with a specified <a href="../manual/MonetizationPlacements.html">Placement</a>, but fires the <c>loadCallback</c> callback on successful load, and the <c>errorCallback</c> callback on failure to load.
  296. /// </summary>
  297. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  298. /// <param name="options">A collection of options that notify the SDK of events when loading the banner.</param>
  299. public static void Load(string placementId, BannerLoadOptions options)
  300. {
  301. s_Platform.Banner.Load(placementId, options);
  302. }
  303. /// <summary>
  304. /// Displays the banner ad with the default <a href="../manual/MonetizationPlacements.html">Placement</a>, and no callbacks.
  305. /// </summary>
  306. public static void Show()
  307. {
  308. Show(null, null);
  309. }
  310. /// <summary>
  311. /// Displays the banner ad with the default <a href="../manual/MonetizationPlacements.html">Placement</a>, but fires the <c>showCallback</c> callback if the banner is visible, and the <c>hideCallback</c> if it isn't.
  312. /// </summary>
  313. /// <param name="options">A collection of options that notify the SDK of events when displaying the banner.</param>
  314. public static void Show(BannerOptions options)
  315. {
  316. Show(null, options);
  317. }
  318. /// <summary>
  319. /// Displays the banner ad with a specified <a href="../manual/MonetizationPlacements.html">Placement</a>, and no callbacks.
  320. /// </summary>
  321. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  322. public static void Show(string placementId)
  323. {
  324. Show(placementId, null);
  325. }
  326. /// <summary>
  327. /// Displays the banner ad with a specified <a href="../manual/MonetizationPlacements.html">Placement</a>, but fires the <c>showCallback</c> callback if the banner is visible, and the <c>hideCallback</c> if it isn't.
  328. /// </summary>
  329. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  330. /// <param name="options">A collection of options that notify the SDK of events when displaying the banner.</param>
  331. public static void Show(string placementId, BannerOptions options)
  332. {
  333. s_Platform.Banner.Show(string.IsNullOrEmpty(placementId) ? null : placementId, options);
  334. }
  335. /// <summary>
  336. /// Allows you to hide a banner ad, instead of destroying it altogether.
  337. /// </summary>
  338. public static void Hide(bool destroy = false)
  339. {
  340. s_Platform.Banner.Hide(destroy);
  341. }
  342. /// <summary>
  343. /// <para>Sets the position of the banner ad, using the <a href="../api/UnityEngine.Advertisements.BannerPosition.html"><c>BannerPosition</c></a> enum.</para>
  344. /// <para>Banner position defaults to <c>BannerPosition.BOTTOM_CENTER</c>.</para>
  345. /// </summary>
  346. /// <param name="position">An enum representing the on-screen anchor position of the banner ad.</param>
  347. public static void SetPosition(BannerPosition position)
  348. {
  349. s_Platform.Banner.SetPosition(position);
  350. }
  351. /// <summary>
  352. /// Returns <c>true</c> if a banner is currently available, and <c>false</c> if it isn't.
  353. /// </summary>
  354. public static bool isLoaded => s_Platform.Banner.IsLoaded;
  355. }
  356. }
  357. }