using System; using UnityEngine.Advertisements.Platform; using UnityEngine.Advertisements.Utilities; namespace UnityEngine.Advertisements { /// /// The wrapper class used to interact with the Unity Ads SDK. /// public static class Advertisement { private static IPlatform s_Platform; static Advertisement() { if (s_Platform == null) { s_Platform = CreatePlatform(); } } /// /// Returns true if the SDK is initialized successfully, and false if it isn't. /// public static bool isInitialized => s_Platform.IsInitialized; /// /// Returns true if the SDK is supported on the current platform, and false if it isn't. /// public static bool isSupported => IsSupported(); /// /// Returns true if the SDK is is in debug mode, and false if it isn't. Debug mode controls the level of logging from the SDK. /// public static bool debugMode { get => s_Platform.DebugMode; set => s_Platform.DebugMode = value; } /// /// Returns the current SDK version. /// public static string version => s_Platform.Version; /// /// Returns true if an ad is currently showing, and false if it isn't. /// public static bool isShowing => s_Platform.IsShowing; /// /// Initializes the SDK with a specified Game ID. /// /// The platform-specific Unity game identifier for your Project, found on the developer dashboard. public static void Initialize(string gameId) { Initialize(gameId, false, false); } /// /// Initializes the SDK with a specified Game ID and test mode setting. /// /// The platform-specific Unity game identifier for your Project, found on the developer dashboard. /// Test mode allows you to test your integration without serving live ads. Use true to initialize in test mode. public static void Initialize(string gameId, bool testMode) { Initialize(gameId, testMode, false); } /// /// Initializes the SDK with a specified Game ID and test mode setting. /// /// The platform-specific Unity game identifier for your Project, found on the developer dashboard. /// Test mode allows you to test your integration without serving live ads. Use true to initialize in test mode. /// Enable the load API lifecycle. See for more information. public static void Initialize(string gameId, bool testMode, bool enablePerPlacementLoad) { Initialize(gameId, testMode, enablePerPlacementLoad, null); } /// /// Initializes the SDK with a specified Game ID, test mode setting, and Placement load setting. /// /// The platform-specific Unity game identifier for your Project, found on the developer dashboard. /// Test mode allows you to test your integration without serving live ads. Use true to initialize in test mode. /// Enable the load API lifecycle. See for more information. /// Listener for IUnityAdsInitializationListener callbacks public static void Initialize(string gameId, bool testMode, bool enablePerPlacementLoad, IUnityAdsInitializationListener initializationListener) { #if UNITY_2020_1_OR_NEWER && ENABLE_CLOUD_SERVICES_ADS && (UNITY_IOS || UNITY_ANDROID || UNITY_EDITOR) if (testMode == false && UnityAdsSettings.testMode) { Debug.Log("Unity Ads is initializing in test mode since test mode is enabled in Service Window."); } s_Platform.Initialize(gameId, UnityAdsSettings.testMode || testMode, enablePerPlacementLoad, initializationListener); #else s_Platform.Initialize(gameId, testMode, enablePerPlacementLoad, initializationListener); #endif } /// /// Returns true if an ad is available to display on the default Placement, and false if it isn't. /// public static bool IsReady() { return s_Platform.IsReady(null); } /// /// Returns true if an ad is available to display for a specified Placement, and false if it isn't. /// /// The unique identifier for a specific Placement, found on the developer dashboard. public static bool IsReady(string placementId) { return s_Platform.IsReady(placementId); } /// /// Loads ad content for a specified Placement. If you initialized the SDK with enablePerPlacementLoad enabled, you must call Load before calling Show. Note that the Load 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. /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// /// public static void Load(string placementId) { Load(placementId, null); } /// /// Loads ad content for a specified Placement. If you initialized the SDK with enablePerPlacementLoad enabled, you must call Load before calling Show. Note that the Load 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. /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// A listener for IUnityAdsLoadListener callbacks /// /// public static void Load(string placementId, IUnityAdsLoadListener loadListener) { s_Platform.Load(placementId, loadListener); } /// /// Displays an ad in the default Ad Unit or Placement if it is ready. /// /// public static void Show() { s_Platform.Show(); } /// /// Displays an ad in the default Placement if it is ready, and passes a ShowResult enum to the ShowOptions.resultCallback callback when the ad finishes. /// /// A collection of options, including resultCallback, for modifying ad behavior. public static void Show(ShowOptions showOptions) { Show(null, showOptions, null); } /// /// Displays an ad in a specified Placement if it is ready. /// /// /// The unique identifier for a specific Placement, found on the developer dashboard. public static void Show(string placementId) { Show(placementId, null, null); } /// /// Displays an ad in a specified Placement if it is ready, and passes a ShowResult enum to the ShowOptions.resultCallback callback when the ad finishes. /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// A collection of options, including resultCallback, for modifying ad behavior. public static void Show(string placementId, ShowOptions showOptions) { Show(placementId, showOptions, null); } /// /// Displays an ad in a specified Placement if it is ready. /// /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// A listener for IUnityAdsShowListener callbacks public static void Show(string placementId, IUnityAdsShowListener showListener) { Show(placementId, null, showListener); } /// /// Displays an ad in a specified Placement if it is ready, and passes a ShowResult enum to the ShowOptions.resultCallback callback when the ad finishes. /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// A collection of options, including resultCallback, for modifying ad behavior. /// A listener for IUnityAdsShowListener callbacks public static void Show(string placementId, ShowOptions showOptions, IUnityAdsShowListener showListener) { s_Platform.Show(placementId, showOptions, showListener); } /// /// Sets various metadata for the SDK. /// /// A metadata container. public static void SetMetaData(MetaData metaData) { s_Platform.SetMetaData(metaData); } /// /// 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. /// /// A listener for Unity Ads callbacks. public static void AddListener(IUnityAdsListener listener) { s_Platform?.AddListener(listener); } /// /// Allows you to remove an active listener. /// /// A listener for Unity Ads callbacks. public static void RemoveListener(IUnityAdsListener listener) { s_Platform?.RemoveListener(listener); } /// /// Returns the state of the default Placement. /// public static PlacementState GetPlacementState() { return s_Platform.GetPlacementState(null); } /// /// Returns the state of a specified Placement. /// /// The unique identifier for a specific Placement, found on the developer dashboard. public static PlacementState GetPlacementState(string placementId) { return s_Platform.GetPlacementState(string.IsNullOrEmpty(placementId) ? null : placementId); } private static IPlatform CreatePlatform() { try { IUnityLifecycleManager unityLifecycleManager = new UnityLifecycleManager(); INativePlatform nativePlatform; INativeBanner nativeBanner; #if UNITY_EDITOR nativeBanner = new Platform.Editor.EditorBanner(); nativePlatform = new Platform.Editor.EditorPlatform(); #elif UNITY_ANDROID nativeBanner = new Platform.Android.AndroidBanner(); var androidPlatform = new Platform.Android.AndroidPlatform(); unityLifecycleManager.SetOnApplicationQuitCallback(() => { androidPlatform.RemoveListener(); }); nativePlatform = androidPlatform; #elif UNITY_IOS nativeBanner = new Platform.iOS.IosBanner(); nativePlatform = new Platform.iOS.IosPlatform(); #else nativeBanner = new Platform.Unsupported.UnsupportedBanner(); nativePlatform = new Platform.Unsupported.UnsupportedPlatform(); #endif IBanner banner = new Advertisements.Banner(nativeBanner, unityLifecycleManager); return new Platform.Platform(nativePlatform, banner, unityLifecycleManager); } catch (Exception exception) { try { Debug.LogError("Initializing Unity Ads."); Debug.LogError(exception.Message); } catch (MissingMethodException) { } var unsupportedPlatform = new Platform.Unsupported.UnsupportedPlatform(); var coroutineExecutor = new UnityLifecycleManager(); var unsupportedBanner = new Platform.Unsupported.UnsupportedBanner(); var genericBanner = new Advertisements.Banner(unsupportedBanner, coroutineExecutor); return new Platform.Platform(unsupportedPlatform, genericBanner, coroutineExecutor); } } private static bool IsSupported() { return Application.isEditor || Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer; } /// /// A static class for implementing banner ads. /// public static class Banner { /// /// Loads the banner ad with the default Placement, and no callbacks. /// public static void Load() { Load(null, null); } /// /// Loads the banner ad with the default Placement, but fires the loadCallback callback on successful load, and the errorCallback callback on failure to load. /// /// A collection of options that notify the SDK of events when loading the banner. public static void Load(BannerLoadOptions options) { Load(null, options); } /// /// Loads the banner ad with a specified Placement, and no callbacks. /// /// The unique identifier for a specific Placement, found on the developer dashboard. public static void Load(string placementId) { Load(placementId, null); } /// /// Loads the banner ad with a specified Placement, but fires the loadCallback callback on successful load, and the errorCallback callback on failure to load. /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// A collection of options that notify the SDK of events when loading the banner. public static void Load(string placementId, BannerLoadOptions options) { s_Platform.Banner.Load(placementId, options); } /// /// Displays the banner ad with the default Placement, and no callbacks. /// public static void Show() { Show(null, null); } /// /// Displays the banner ad with the default Placement, but fires the showCallback callback if the banner is visible, and the hideCallback if it isn't. /// /// A collection of options that notify the SDK of events when displaying the banner. public static void Show(BannerOptions options) { Show(null, options); } /// /// Displays the banner ad with a specified Placement, and no callbacks. /// /// The unique identifier for a specific Placement, found on the developer dashboard. public static void Show(string placementId) { Show(placementId, null); } /// /// Displays the banner ad with a specified Placement, but fires the showCallback callback if the banner is visible, and the hideCallback if it isn't. /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// A collection of options that notify the SDK of events when displaying the banner. public static void Show(string placementId, BannerOptions options) { s_Platform.Banner.Show(string.IsNullOrEmpty(placementId) ? null : placementId, options); } /// /// Allows you to hide a banner ad, instead of destroying it altogether. /// public static void Hide(bool destroy = false) { s_Platform.Banner.Hide(destroy); } /// /// Sets the position of the banner ad, using the BannerPosition enum. /// Banner position defaults to BannerPosition.BOTTOM_CENTER. /// /// An enum representing the on-screen anchor position of the banner ad. public static void SetPosition(BannerPosition position) { s_Platform.Banner.SetPosition(position); } /// /// Returns true if a banner is currently available, and false if it isn't. /// public static bool isLoaded => s_Platform.Banner.IsLoaded; } } }