#if UNITY_EDITOR using System; using Azerion.BlueStack.API; using Azerion.BlueStack.API.Banner; using Azerion.BlueStack.Internal; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace Azerion.BlueStack.Platforms.UnityEditor { public class BannerAdClient : BaseAdClient, IBannerAdClient, IDisposable { private string _placementId; private AdPosition _adPosition; private AdSize _adSize; // Fires when the banner ad is loaded public event EventHandler OnBannerDidLoad; // Fires when the banner ad has failed to load public event EventHandler OnBannerDidFailed; // Fires when the banner ad is clicked public event EventHandler OnAdClicked; // Fires when the banner ad is refreshed public event EventHandler OnBannerDidRefresh; // Fires when the banner ad has failed to refresh public event EventHandler OnBannerDidFailToRefresh; // Fires when the banner ad has been hidden public event EventHandler OnBannerHide; // Fires when the banner ad is displayed public event EventHandler OnBannerDisplay; private static readonly string _adPrefabsDir = "Packages/com.azerion.bluestack/Editor/Prefabs/AdPrefabs/"; private Dictionary _prefabAds = new Dictionary() { { AdSize.Banner, _adPrefabsDir + "Banners/Banner.prefab" }, { AdSize.DynamicBanner, _adPrefabsDir + "Banners/DynamicBanner.prefab" }, { AdSize.MediumRectangle, _adPrefabsDir + "Banners/MediumRectangle.prefab" }, { AdSize.FullBanner, _adPrefabsDir + "Banners/FullBanner.prefab" }, { AdSize.Leaderboard, _adPrefabsDir + "Banners/Leaderboard.prefab" }, { AdSize.LargeBanner, _adPrefabsDir + "Banners/LargeBanner.prefab" }, }; // Creates a banner with a custom position. public void CreateBannerAd(string placementId, AdPosition adPosition) { this._placementId = placementId; this._adPosition = adPosition; } // Loads a banner and adds it to the view hierarchy. public void Load(AdSize adSize) { Debug.Log("Load banner AdSize: " + adSize.Size); this._adSize = adSize; LoadAndSetAdPrefab(_prefabAds[adSize]); if (adPrefab != null) { if (adSize == AdSize.DynamicBanner) { SetAndStretchAd(adPrefab, this._adPosition, adSize); } else { AnchorAd(adPrefab, this._adPosition); } OnBannerDidLoad?.Invoke(this, EventArgs.Empty); } else { OnBannerDidFailed?.Invoke(this, new BlueStackError(-1, "Ad could not be loaded")); } } // Loads a banner with preference. NOT implemented for the editor public void Load(AdSize adSize, IPreferenceClient iPreferenceClient) { Load(adSize); } // Shows the banner on the screen public void Show() { if (adPrefab != null) { dummyAd = AdBehaviour.ShowAd(adPrefab, GetRectTransform(adPrefab).anchoredPosition); OnBannerDisplay?.Invoke(this, EventArgs.Empty); AddClickBehavior(dummyAd); } else { Debug.Log("Ad could not be shown"); } } // To check refresh events in the editor public void Refresh(bool shouldSucceed) { if (shouldSucceed) { OnBannerDidRefresh?.Invoke(this, EventArgs.Empty); } else { OnBannerDidFailToRefresh?.Invoke(this, new BlueStackError(-1, "Ad failed to refresh")); } } // Hides the banner from the screen public void Hide() { AdBehaviour.DestroyAd(dummyAd); OnBannerHide?.Invoke(this, EventArgs.Empty); } // Destroys a banner public void Destroy() { AdBehaviour.DestroyAd(dummyAd); adPrefab = null; } public void SetPosition(AdPosition adPosition) { if (dummyAd != null) { AnchorAd(dummyAd, adPosition); } else { Debug.Log("Banner not found"); } } private void SetAndStretchAd(GameObject dummyAd, AdPosition pos, AdSize adSize) { if (dummyAd != null) { Image myImage = dummyAd.GetComponentInChildren(); RectTransform rect = myImage.GetComponentInChildren(); rect.pivot = new Vector2(0.5f, 0.5f); if (pos == AdPosition.Bottom) { rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, rect.sizeDelta.y); rect.anchoredPosition = new Vector2(0, (float)rect.sizeDelta.y / 2); } else if (pos == AdPosition.Top) { rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, rect.sizeDelta.y); rect.anchoredPosition = new Vector2(0, -(float)rect.sizeDelta.y / 2); } else { rect.anchoredPosition = rect.position; } } else { Debug.Log("Invalid Ad Prefab"); } } private void AnchorAd(GameObject dummyAd, AdPosition position) { if (dummyAd != null) { Image myImage = dummyAd.GetComponentInChildren(); RectTransform rect = myImage.GetComponentInChildren(); float x = (float)rect.sizeDelta.x / 2; float y = (float)rect.sizeDelta.y / 2; switch (position) { case (AdPosition.Top): rect.pivot = new Vector2(0.5f, 0.5f); rect.anchorMin = new Vector2(0.5f, 1); rect.anchorMax = new Vector2(0.5f, 1); rect.anchoredPosition = new Vector2(0, -y); break; case (AdPosition.Bottom): rect.pivot = new Vector2(0.5f, 0.5f); rect.anchorMin = new Vector2(0.5f, 0); rect.anchorMax = new Vector2(0.5f, 0); rect.anchoredPosition = new Vector2(0, y); break; default: rect.pivot = new Vector2(0.5f, 0.5f); rect.anchorMin = new Vector2(0.5f, 0.5f); rect.anchorMax = new Vector2(0.5f, 0.5f); rect.anchoredPosition = new Vector2(0, 0); break; } } else { Debug.Log("Invalid Ad Prefab"); } } private void AddClickBehavior(GameObject dummyAd) { Image myImage = dummyAd.GetComponentInChildren(); Button button = myImage.GetComponentInChildren