I followed the steps of adding the Admob packages to unity. I am able to use the Banner and Interstitial ads. However, I would also like to use Native ads as well. So I install the GoogleMobileAds-native to unity. However, when I run my application on Unity, I am not seeing the ad. Here is what I did:
I notice that I was not able to use the UnifiedNativeAd as it could not be found. So instead I am using NativeAd. Any help would be appreciated
using System.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Threading.Tasks;
using Debug = UnityEngine.Debug;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
public class AdMobManager : MonoBehaviour
{
private NativeAd adNative;
private string idApp, idNative;
public GameObject adNativePanel;
public RawImage adIcon;
public RawImage adChoices;
public Text adHeadline;
public Text adCallToAction;
public Text adAdvertiser;
// Start is called before the first frame update
void Start(){
idApp = "ca-app-pub-3940256099942544/6300978111";
idNative = "ca-app-pub-3940256099942544/2247696110";
List deviceIds = new List() { AdRequest.TestDeviceSimulator };
// Add some test device IDs (replace with your own device IDs).
#if UNITY_ANDROID
deviceIds.Add(idApp);
#endif
// Configure TagForChildDirectedTreatment and test device IDs.
RequestConfiguration requestConfiguration =
new RequestConfiguration.Builder()
.SetTagForChildDirectedTreatment(TagForChildDirectedTreatment.Unspecified)
.SetTestDeviceIds(deviceIds).build();
MobileAds.SetRequestConfiguration(requestConfiguration);
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(HandleInitCompleteAction);
}
void Update(){
Texture2D iconTexture = this.adNative.GetIconTexture();
Texture2D iconAdChoices = this.adNative.GetAdChoicesLogoTexture();
string headline = this.adNative.GetHeadlineText();
string cta = this.adNative.GetCallToActionText();
string advertiser = this.adNative.GetAdvertiserText();
adIcon.texture = iconTexture;
adChoices.texture = iconAdChoices;
adHeadline.text = headline;
adAdvertiser.text = advertiser;
adCallToAction.text = cta;
//register gameobjects
adNative.RegisterIconImageGameObject(adIcon.gameObject);
adNative.RegisterAdChoicesLogoGameObject(adChoices.gameObject);
adNative.RegisterHeadlineTextGameObject(adHeadline.gameObject);
adNative.RegisterCallToActionGameObject(adCallToAction.gameObject);
adNative.RegisterAdvertiserTextGameObject(adAdvertiser.gameObject);
adNativePanel.SetActive(true); //show ad panel
}
private void HandleInitCompleteAction(InitializationStatus initstatus) {
Debug.Log("Initialization complete.");
// Callbacks from GoogleMobileAds are not guaranteed to be called on
// the main thread.
// In this example we use MobileAdsEventExecutor to schedule these calls on
// the next Update() loop.
MobileAdsEventExecutor.ExecuteInUpdate(() => {
RequestNativeAd();
});
}
private void RequestNativeAd(){
AdLoader adLoader = new AdLoader.Builder (idNative).ForNativeAd().Build();
adLoader.OnNativeAdLoaded += this.HandleOnUnifiedNativeAdLoaded;
adLoader.LoadAd(AdRequestBuild());
}
//events
private void HandleOnUnifiedNativeAdLoaded (object sender, NativeAdEventArgs args){
this.adNative = args.nativeAd;
}
AdRequest AdRequestBuild(){
return new AdRequest.Builder().Build();
}
}
↧