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); } /// /// 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, 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. /// Listener for IUnityAdsInitializationListener callbacks public static void Initialize(string gameId, bool testMode, IUnityAdsInitializationListener initializationListener) { if (initializationListener == null) { Debug.LogError("initializationListener is null, you will not receive any callbacks"); } s_Platform.Initialize(gameId, testMode, new UnityAdsInitializationListenerMainDispatch(initializationListener, s_Platform.UnityLifecycleManager)); } /// /// Loads ad content for a specified Placement. /// /// 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. /// /// 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) { if (loadListener == null) { Debug.LogError("loadListener is null, you will not receive any callbacks"); } s_Platform.Load(placementId, new UnityAdsLoadListenerMainDispatch(loadListener, s_Platform.UnityLifecycleManager)); } /// /// Displays an ad in the default <-a href="../manual/MonetizationPlacements.html">Placement if it is ready. /// /// A collection of options for modifying ad behaviour. [Obsolete("ShowOptions has been deprecated and no longer has callbacks. It's only function currently is to pass gamerSid")] 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. /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// A collection of options for modifying ad behaviour. 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 /// /// The unique identifier for a specific Placement, found on the developer dashboard. /// A collection of options for modifying ad behaviour. /// A listener for IUnityAdsShowListener callbacks public static void Show(string placementId, ShowOptions showOptions, IUnityAdsShowListener showListener) { if (showListener == null) { Debug.LogError("showListener is null, you will not receive any callbacks"); } s_Platform.Show(placementId, showOptions, new UnityAdsShowListenerMainDispatch(showListener, s_Platform.UnityLifecycleManager)); } /// /// Sets various metadata for the SDK. /// /// A metadata container. public static void SetMetaData(MetaData metaData) { s_Platform.SetMetaData(metaData); } 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(); nativePlatform = new Platform.Android.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 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) { if (string.IsNullOrEmpty((placementId))) { Debug.LogWarning("placementId is empty"); } 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; } } }