using System; using System.Collections.Generic; using System.Linq; using Azerion.BlueStack.Common; using Azerion.BlueStack.Internal; using UnityEngine; namespace Azerion.BlueStack.API { public class NativeAdInteractionHandler { private INativeAdClient _nativeAdClient; private List _nativeAdObjects; private INativeAdInteractionHandlerClient _nativeAdInteractionHandlerClient; private string _clickUrl; public NativeAdInteractionHandler(INativeAdClient nativeAdClient) { _nativeAdObjects = new List(); _nativeAdClient = nativeAdClient; } public bool RegisterImageGameObjects(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.Image); } public bool RegisterIconImageGameObject(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.Icon, true, true); } public bool RegisterAdChoicesLogoGameObject(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.AdChoices); } public bool RegisterHeadlineTextGameObject(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.Headline, true); } public bool RegisterBodyTextGameObject(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.Body); } public bool RegisterCallToActionGameObject(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.CallToAction, true, true); } public bool RegisterAdvertiserTextGameObject(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.Advertiser, true); } public bool RegisterPriceGameObject(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.Price); } public bool RegisterStoreGameObject(GameObject gameObject) { return RegisterGameObject(gameObject, NativeAssetID.Store); } private bool RegisterGameObject(GameObject adObject, NativeAssetID assetID, bool isImpressive = false, bool isClickable = false) { return Register(adObject, assetID.Value, isImpressive, isClickable); // replace with GetAssetIDMappings } private bool Register(GameObject adObject, string assetID, bool isImpressive, bool isClickable) { NativeAdObject nativeAdObject = adObject.GetComponent(); if (nativeAdObject != null) { UnityEngine.Object.DestroyImmediate(nativeAdObject); } nativeAdObject = adObject.AddComponent(); _nativeAdObjects.Add(nativeAdObject); nativeAdObject.OnAdDisplay += RecordImpression; nativeAdObject.OnAdClick += RecordClick; try { nativeAdObject.Initialize(assetID, isImpressive, isClickable); } catch (SystemException ex) { Debug.LogWarning("Unable to initialize nativeAdObject!"); Debug.LogException(ex); UnityEngine.Object.Destroy(nativeAdObject); _nativeAdObjects.Remove(nativeAdObject); return false; } return true; } private bool _impressionComplete = false; // private bool _clickComplete = false; // TODO - check from native side internal void SetClickUrl(string url) { _clickUrl = url; } // Handle Ad Impression - Incomplete private void RecordImpression(object sender, EventArgs eventArgs) { List requiredAssets = NativeAssetInfo.RequiredAssets; var successfulImpressiveAssets = _nativeAdObjects .Where(obj => requiredAssets.Contains(obj.ObjectTag) && obj.HasMadeImpression) .Select(obj => obj.ObjectTag); _impressionComplete = requiredAssets.All(tag => successfulImpressiveAssets.Contains(tag)); if (!_impressionComplete) return; if (sender is NativeAdObject nativeAdObject) { _nativeAdClient.OnNativeAdImpression += (sender, args) => { foreach (NativeAdObject adObject in _nativeAdObjects) { if (adObject != null) { AdsEventExecutor.ExecuteInUpdate(delegate { adObject.StopImpressionCheck(); }); } } }; _nativeAdClient.RecordImpression(); } } // Handle Ad Clicked - Incomplete private void RecordClick(object sender, ClickEventArgs clickEventArgs) { Debug.Log("Record Click!"); if (sender is NativeAdObject nativeAdObject) { _nativeAdClient.OnNativeAdClicked += (sender, args) => { // OpenURL(); Debug.Log("InteractionHandlerClient: OnNativeAdClicked!"); }; _nativeAdClient.PerformClick(); } } public void OpenURL() { Debug.Log("Opened URL"); Application.OpenURL(_clickUrl); } } }