Unable to view Native Ad on Unity C#

Viewed 32

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. I followed the following instruction provided in this link.

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.

Update: Still have not been able to figure out the issue. 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;
    private bool nativeAdLoaded;

    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 = "75EF8D155528C04DACBBA6F36F433035";
        idNative = "ca-app-pub-3940256099942544/2247696110";

        List<String> deviceIds = new List<String>() { 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);
    }

    private void HandleInitCompleteAction(InitializationStatus initstatus) {
        Debug.Log("Initialization complete.");

        MobileAdsEventExecutor.ExecuteInUpdate(() => {
            RequestNativeAd();
        });
    }

    void Update(){
        if (this.nativeAdLoaded) {
            Debug.Log("Setting items");
            this.nativeAdLoaded = false;
            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 RequestNativeAd(){
        Debug.Log("RequestNativeAd function");
        AdLoader adLoader = new AdLoader.Builder(idNative).ForNativeAd().Build();
        adLoader.OnAdLoaded += this.HandleOnUnifiedNativeAdLoaded;
        adLoader.OnAdFailedToLoad += this.HandleNativeAdFailedToLoad;
        adLoader.LoadAd(AdRequestBuild());
    }

    //events
    private void HandleOnUnifiedNativeAdLoaded (object sender, NativeAdEventArgs args){
        Debug.Log("Native ad loaded.");
        this.adNative = args.nativeAd;
        nativeAdLoaded = true;
    }

    //events
    private void HandleNativeAdFailedToLoad(object sender, AdFailedToLoadEventArgs args){
        Debug.Log("Native ad failed to load: " + args);
    }

    AdRequest AdRequestBuild(){
        Debug.Log("AdRequestBuild function");
        return new AdRequest.Builder().Build();
    }
}
0 Answers
Related