using System; using UnityEngine; using Azerion.BlueStack.API; using UnityEngine.UI; namespace Azerion.BlueStack.Example { public class SmallNativeAdManager : MonoBehaviour { private NativeAd nativeAd; private bool unifiedNativeAdLoaded; private Preference _preference; private string AppId { get { #if UNITY_IOS return "5277155"; #elif UNITY_ANDROID return "5277155"; #else return "unexpected_platform"; #endif } } private string NativeAdUnitId { get { #if UNITY_IOS return "/" + AppId + "/native"; #elif UNITY_ANDROID return "/" + AppId + "/native"; #else return "unexpected_platform"; #endif } } // Below are the UI element that you need to assign from Unity Editor public GameObject badge; public GameObject appIcon; public GameObject title; public GameObject callToAction; // Use this for initialization void Start() { Settings settings = new Settings(isDebugModeEnabled: true); BlueStackAds.Initialize(AppId, settings, HandleSDKInitCompleteAction); _preference = CreatePreference(); } private void HandleSDKInitCompleteAction(SDKInitializationStatus sdkInitializationStatus) { bool isSuccess = sdkInitializationStatus.IsSuccess; string description = sdkInitializationStatus.Description; if (isSuccess) { Debug.Log("BlueStack SDK initialization success: " + description); RequestNativeAd(); } else { Debug.Log("BlueStack SDK initialization failed: " + description); } } // Update is called once per frame void Update() { if (this.unifiedNativeAdLoaded) { this.unifiedNativeAdLoaded = false; Debug.Log("NativeAdManager: RegisterGameObjects"); // badge string badgeText = this.nativeAd.GetBadgeText(); if (badgeText != null) { Debug.Log("NativeAdManager: Register AdChoices"); this.badge.GetComponent().text = badgeText; if (!this.nativeAd.RegisterBadgeTextGameObject(this.badge)) { Debug.Log("RegisterBadgeTextGameObject Unsuccessful"); } } // Icon Texture Texture2D iconTexture = this.nativeAd.GetIconTexture(); if (iconTexture != null) { Debug.Log("NativeAdManager: Register Icon Texture"); appIcon.GetComponent().texture = iconTexture; if (!this.nativeAd.RegisterIconImageGameObject(appIcon)) { Debug.Log("RegisterIconImageGameObject Unsuccessful"); } } // Headline/Title string titleText = this.nativeAd.GetTitleText(); if (titleText != null) { Debug.Log("NativeAdManager: Register Head line"); title.GetComponent().text = titleText; if (!this.nativeAd.RegisterTitleTextGameObject(title)) { Debug.Log("RegisterHeadlineTextGameObject Unsuccessful"); } } // CallToAction string callToActionText = this.nativeAd.GetCallToActionText(); if (callToActionText != null) { Debug.Log("NativeAdManager: Register CallToAction"); callToAction.GetComponent().text = callToActionText; if (!this.nativeAd.RegisterCallToActionTextGameObject(callToAction)) { Debug.Log("RegisterCallToActionGameObject Unsuccessful"); } } // Debug.Log("Headline is " + titleText); // Debug.Log("GetBodyText is " + this.nativeAd.GetBodyText()); // Debug.Log("GetCallToActionText is " + callToActionText); } } private void RequestNativeAd() { Debug.Log("NativeAdManager: Requesting Ad!"); NativeAdLoader nativeAdLoader = new NativeAdLoader(NativeAdUnitId); nativeAdLoader.OnNativeAdLoaded += this.HandleNativeAdLoaded; nativeAdLoader.OnNativeAdFailedToLoad += this.HandleNativeNativeAdFailedToLoad; nativeAdLoader.Load(); } private void HandleNativeNativeAdFailedToLoad(object sender, BlueStackError args) { Debug.Log("BlueStack NativeAd failed to load: " + "errorCode: " + args.ErrorCode + " message: " + args.Message); } private void HandleNativeAdLoaded(object sender, NativeAdEventArgs args) { Debug.Log("NativeAdManager: Unified Native Ad Loaded"); this.nativeAd = args.nativeAd; this.unifiedNativeAdLoaded = true; this.nativeAd.OnNativeAdImpression += this.HandleNativeAdImpression; this.nativeAd.OnNativeAdClicked += this.HandleNativeAdClicked; // RegisterGameObjects(); } private void HandleNativeAdImpression(object sender, EventArgs args) { Debug.Log("NativeAdManager: Native Ad Impression successful"); } private void HandleNativeAdClicked(object sender, EventArgs args) { Debug.Log("NativeAdManager: Unified Native Ad click successful"); } private Preference CreatePreference() { Preference bsPreference = new Preference(); Location myLocation = new Location(Location.NONE_PROVIDER) { Latitude = 35.757866, Longitude = 10.810547 }; bsPreference.SetAge(25); bsPreference.SetLanguage("en"); bsPreference.SetGender(Gender.Male); bsPreference.SetKeyword("brand=myBrand;category=sport"); bsPreference.SetLocation(myLocation, 1); bsPreference.SetContentUrl("https://madvertise.com/en/"); return bsPreference; } } }