Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Advertisement.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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);
  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, null);
  59. }
  60. /// <summary>
  61. /// Initializes the SDK with a specified Game ID, test mode setting, and Placement load setting.
  62. /// </summary>
  63. /// <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>
  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="initializationListener">Listener for IUnityAdsInitializationListener callbacks</param>
  66. public static void Initialize(string gameId, bool testMode, IUnityAdsInitializationListener initializationListener)
  67. {
  68. if (initializationListener == null)
  69. {
  70. Debug.LogError("initializationListener is null, you will not receive any callbacks");
  71. }
  72. s_Platform.Initialize(gameId, testMode, new UnityAdsInitializationListenerMainDispatch(initializationListener, s_Platform.UnityLifecycleManager));
  73. }
  74. /// <summary>
  75. /// Loads ad content for a specified Placement.
  76. /// </summary>
  77. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  78. /// <seealso cref="Advertisement.Initialize(string, bool, bool)"/>
  79. /// <seealso cref="Advertisement.Show(string)"/>
  80. public static void Load(string placementId)
  81. {
  82. Load(placementId, null);
  83. }
  84. /// <summary>
  85. /// Loads ad content for a specified Placement.
  86. /// </summary>
  87. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  88. /// <param name="loadListener">A listener for <c>IUnityAdsLoadListener</c> callbacks</param>
  89. /// <seealso cref="Advertisement.Initialize(string, bool, bool)"/>
  90. /// <seealso cref="Advertisement.Show(string)"/>
  91. public static void Load(string placementId, IUnityAdsLoadListener loadListener)
  92. {
  93. if (loadListener == null)
  94. {
  95. Debug.LogError("loadListener is null, you will not receive any callbacks");
  96. }
  97. s_Platform.Load(placementId, new UnityAdsLoadListenerMainDispatch(loadListener, s_Platform.UnityLifecycleManager));
  98. }
  99. /// <summary>
  100. /// Displays an ad in the default <-a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready.
  101. /// </summary>
  102. /// <param name="showOptions">A collection of options for modifying ad behaviour.</param>
  103. [Obsolete("ShowOptions has been deprecated and no longer has callbacks. It's only function currently is to pass gamerSid")]
  104. public static void Show(ShowOptions showOptions)
  105. {
  106. Show(null, showOptions, null);
  107. }
  108. /// <summary>
  109. /// Displays an ad in a specified <a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready.
  110. /// </summary>
  111. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  112. public static void Show(string placementId)
  113. {
  114. Show(placementId, null, null);
  115. }
  116. /// <summary>
  117. /// Displays an ad in a specified <a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready.
  118. /// </summary>
  119. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  120. /// <param name="showOptions">A collection of options for modifying ad behaviour.</param>
  121. public static void Show(string placementId, ShowOptions showOptions)
  122. {
  123. Show(placementId, showOptions, null);
  124. }
  125. /// <summary>
  126. /// Displays an ad in a specified <a href="../manual/MonetizationPlacements.html">Placement</a> if it is ready.
  127. /// </summary>
  128. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  129. /// <param name="showListener">A listener for <c>IUnityAdsShowListener</c> callbacks</param>
  130. public static void Show(string placementId, IUnityAdsShowListener showListener)
  131. {
  132. Show(placementId, null, showListener);
  133. }
  134. /// <summary>
  135. /// Displays an ad in a specified <a href="../manual/MonetizationPlacements.html">Placement</a>
  136. /// </summary>
  137. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  138. /// <param name="showOptions">A collection of options for modifying ad behaviour.</param>
  139. /// <param name="showListener">A listener for <c>IUnityAdsShowListener</c> callbacks</param>
  140. public static void Show(string placementId, ShowOptions showOptions, IUnityAdsShowListener showListener)
  141. {
  142. if (showListener == null)
  143. {
  144. Debug.LogError("showListener is null, you will not receive any callbacks");
  145. }
  146. s_Platform.Show(placementId, showOptions, new UnityAdsShowListenerMainDispatch(showListener, s_Platform.UnityLifecycleManager));
  147. }
  148. /// <summary>
  149. /// Sets various metadata for the SDK.
  150. /// </summary>
  151. /// <param name="metaData">A metadata container.</param>
  152. public static void SetMetaData(MetaData metaData)
  153. {
  154. s_Platform.SetMetaData(metaData);
  155. }
  156. private static IPlatform CreatePlatform()
  157. {
  158. try
  159. {
  160. IUnityLifecycleManager unityLifecycleManager = new UnityLifecycleManager();
  161. INativePlatform nativePlatform;
  162. INativeBanner nativeBanner;
  163. #if UNITY_EDITOR
  164. nativeBanner = new Platform.Editor.EditorBanner();
  165. nativePlatform = new Platform.Editor.EditorPlatform();
  166. #elif UNITY_ANDROID
  167. nativeBanner = new Platform.Android.AndroidBanner();
  168. nativePlatform = new Platform.Android.AndroidPlatform();;
  169. #elif UNITY_IOS
  170. nativeBanner = new Platform.iOS.IosBanner();
  171. nativePlatform = new Platform.iOS.IosPlatform();
  172. #else
  173. nativeBanner = new Platform.Unsupported.UnsupportedBanner();
  174. nativePlatform = new Platform.Unsupported.UnsupportedPlatform();
  175. #endif
  176. IBanner banner = new Advertisements.Banner(nativeBanner, unityLifecycleManager);
  177. return new Platform.Platform(nativePlatform, banner, unityLifecycleManager);
  178. }
  179. catch (Exception exception)
  180. {
  181. try
  182. {
  183. Debug.LogError("Initializing Unity Ads.");
  184. Debug.LogError(exception.Message);
  185. }
  186. catch (MissingMethodException)
  187. {}
  188. var unsupportedPlatform = new Platform.Unsupported.UnsupportedPlatform();
  189. var coroutineExecutor = new UnityLifecycleManager();
  190. var unsupportedBanner = new Platform.Unsupported.UnsupportedBanner();
  191. var genericBanner = new Advertisements.Banner(unsupportedBanner, coroutineExecutor);
  192. return new Platform.Platform(unsupportedPlatform, genericBanner, coroutineExecutor);
  193. }
  194. }
  195. private static bool IsSupported()
  196. {
  197. return Application.isEditor || Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer;
  198. }
  199. /// <summary>
  200. /// A static class for implementing banner ads.
  201. /// </summary>
  202. public static class Banner
  203. {
  204. /// <summary>
  205. /// Loads the banner ad with the default <a href="../manual/MonetizationPlacements.html">Placement</a>, and no callbacks.
  206. /// </summary>
  207. public static void Load()
  208. {
  209. Load(null, null);
  210. }
  211. /// <summary>
  212. /// 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.
  213. /// </summary>
  214. /// <param name="options">A collection of options that notify the SDK of events when loading the banner.</param>
  215. public static void Load(BannerLoadOptions options)
  216. {
  217. Load(null, options);
  218. }
  219. /// <summary>
  220. /// Loads the banner ad with a specified <a href="../manual/MonetizationPlacements.html">Placement</a>, and no callbacks.
  221. /// </summary>
  222. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  223. public static void Load(string placementId)
  224. {
  225. Load(placementId, null);
  226. }
  227. /// <summary>
  228. /// 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.
  229. /// </summary>
  230. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  231. /// <param name="options">A collection of options that notify the SDK of events when loading the banner.</param>
  232. public static void Load(string placementId, BannerLoadOptions options)
  233. {
  234. s_Platform.Banner.Load(placementId, options);
  235. }
  236. /// <summary>
  237. /// Displays the banner ad with a specified <a href="../manual/MonetizationPlacements.html">Placement</a>, and no callbacks.
  238. /// </summary>
  239. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  240. public static void Show(string placementId)
  241. {
  242. Show(placementId, null);
  243. }
  244. /// <summary>
  245. /// 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.
  246. /// </summary>
  247. /// <param name="placementId">The unique identifier for a specific Placement, found on the <a href="https://operate.dashboard.unity3d.com/">developer dashboard</a>.</param>
  248. /// <param name="options">A collection of options that notify the SDK of events when displaying the banner.</param>
  249. public static void Show(string placementId, BannerOptions options)
  250. {
  251. if (string.IsNullOrEmpty((placementId)))
  252. {
  253. Debug.LogWarning("placementId is empty");
  254. }
  255. s_Platform.Banner.Show(string.IsNullOrEmpty(placementId) ? null : placementId, options);
  256. }
  257. /// <summary>
  258. /// Allows you to hide a banner ad, instead of destroying it altogether.
  259. /// </summary>
  260. public static void Hide(bool destroy = false)
  261. {
  262. s_Platform.Banner.Hide(destroy);
  263. }
  264. /// <summary>
  265. /// <para>Sets the position of the banner ad, using the <a href="../api/UnityEngine.Advertisements.BannerPosition.html"><c>BannerPosition</c></a> enum.</para>
  266. /// <para>Banner position defaults to <c>BannerPosition.BOTTOM_CENTER</c>.</para>
  267. /// </summary>
  268. /// <param name="position">An enum representing the on-screen anchor position of the banner ad.</param>
  269. public static void SetPosition(BannerPosition position)
  270. {
  271. s_Platform.Banner.SetPosition(position);
  272. }
  273. /// <summary>
  274. /// Returns <c>true</c> if a banner is currently available, and <c>false</c> if it isn't.
  275. /// </summary>
  276. public static bool isLoaded => s_Platform.Banner.IsLoaded;
  277. }
  278. }
  279. }