using System; using System.Threading.Tasks; using Unity.Services.Mediation.Platform; using UnityEngine; namespace Unity.Services.Mediation { /// /// Class to be instantiated in order to show a Banner Ad. /// public sealed class BannerAd : IBannerAd { /// /// Event to be triggered by the adapter when an Ad is loaded. /// public event EventHandler OnLoaded; /// /// Event to be triggered by the adapter when an Ad fails to load. /// public event EventHandler OnFailedLoad; /// /// Event to be triggered by the adapter when the user clicks on the Ad. /// public event EventHandler OnClicked; /// /// Event to be triggered by the adapter when the Ad refreshes /// public event EventHandler OnRefreshed; /// /// Get the current state of the ad. /// public AdState AdState => m_BannerAdImpl.AdState; /// /// Get the ad unit id set during construction. /// public string AdUnitId => m_BannerAdImpl.AdUnitId; /// /// Get the banner size set during construction. /// public BannerAdSize Size => m_BannerAdImpl.Size; IPlatformBannerAd m_BannerAdImpl; TaskCompletionSource m_LoadCompletionSource; bool m_IsLoading; /// /// Constructor for managing a specific Banner Ad. /// /// Unique Id for the Ad you want to show. /// Size of banner set to be constructed. /// Anchor on which the banner position is based /// The X, Y coordinates offsets, relative to the anchor point public BannerAd(string adUnitId, BannerAdSize size, BannerAdAnchor anchor = BannerAdAnchor.Default, Vector2 positionOffset = new Vector2()) { #if UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS) m_BannerAdImpl = new EditorBannerAd(adUnitId, size, anchor, positionOffset); #elif UNITY_ANDROID m_BannerAdImpl = new AndroidBannerAd(adUnitId, size, anchor, positionOffset); #elif UNITY_IOS m_BannerAdImpl = new IosBannerAd(adUnitId, size, anchor, positionOffset); #else m_BannerAdImpl = new UnsupportedBannerAd(adUnitId, size, anchor, positionOffset); #endif InitializeCallbacks(); } /// /// Sets the position of a banner ad. /// /// Anchor on which the banner position is based /// The X, Y coordinates offsets, relative to the anchor point public void SetPosition(BannerAdAnchor anchor, Vector2 positionOffset = new Vector2()) => m_BannerAdImpl.SetPosition(anchor, positionOffset); internal BannerAd(IPlatformBannerAd bannerAdImpl) { m_BannerAdImpl = bannerAdImpl; InitializeCallbacks(); } void InitializeCallbacks() { m_BannerAdImpl.OnLoaded += (sender, args) => OnLoaded?.Invoke(this, args); m_BannerAdImpl.OnFailedLoad += (sender, args) => OnFailedLoad?.Invoke(this, args); m_BannerAdImpl.OnClicked += (sender, args) => OnClicked?.Invoke(this, args); m_BannerAdImpl.OnRefreshed += (sender, args) => OnRefreshed?.Invoke(this, args); } /// /// Method to tell the Mediation SDK to load an Ad. /// /// Async Load task /// Thrown when the ad failed to load public Task LoadAsync() { if (!m_IsLoading) { SetupAsyncLoad(); m_BannerAdImpl.Load(); } return m_LoadCompletionSource?.Task ?? Task.CompletedTask; } void SetupAsyncLoad() { m_LoadCompletionSource = new TaskCompletionSource(); m_BannerAdImpl.OnLoaded += OnLoadCompleted; m_BannerAdImpl.OnFailedLoad += OnLoadFailed; m_IsLoading = true; } void OnLoadCompleted(object sender, EventArgs e) { m_LoadCompletionSource.TrySetResult(null); TearDownAsyncLoad(); } void OnLoadFailed(object sender, LoadErrorEventArgs args) { m_LoadCompletionSource.SetException(new LoadFailedException(args.Error, args.Message)); TearDownAsyncLoad(); } void TearDownAsyncLoad() { m_BannerAdImpl.OnFailedLoad -= OnLoadFailed; m_BannerAdImpl.OnLoaded -= OnLoadCompleted; m_IsLoading = false; } /// /// Dispose and release internal resources. /// public void Dispose() => m_BannerAdImpl.Dispose(); } }