Native Ad not loading Unity C#

413 views
Skip to first unread message

Roberto Flores

unread,
Sep 25, 2022, 1:42:38 AM9/25/22
to Google Mobile Ads SDK Developers
I have followed the steps to load a Native Ad Admob. However, when running the code, I notice that the following line of codes are not being run and I am not sure why. Any help would be appreciated.

adLoader.OnNativeAdLoaded += this.HandleNativeAdLoaded;
 adLoader.OnAdFailedToLoad += this.HandleNativeAdFailedToLoad;

The following is the code:

    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.OnNativeAdLoaded += this.HandleNativeAdLoaded;
        adLoader.OnAdFailedToLoad += this.HandleNativeAdFailedToLoad;
        adLoader.LoadAd(AdRequestBuild());
    }

    //events
    private void HandleNativeAdLoaded (object sender, NativeAdEventArgs args){
        Debug.Log("Native ad loaded.");
        this.adNative = args.nativeAd;
        this.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();
    }

Mobile Ads SDK Forum Advisor

unread,
Sep 26, 2022, 3:06:21 AM9/26/22
to rbrtfl...@gmail.com, google-adm...@googlegroups.com

Hi Roberto,

 

Thank you for reaching out to us.

 

For us to further check this behavior,  can you provide us the following details below privately? You can provide the following details via Reply privately to author option or directly provide it to the link below.

 

  • App ID
  • Sample app project
  • Steps to replicate
  • SDK version
  • Screen Recording of the behavior

 

If the file(s) you are looking to share are less than 25mb in total you can attach them to this case on your next reply. If you are having trouble attaching your file to this case or if your file(s) are larger than 25mb, you can share your files with me by performing the following steps:

 

1. Navigate to

https://docs.google.com/forms/d/e/1FAIpQLSfkAiXMeYP-fw1W3Z-tT9uwmATEKO5X6S-th0gR2ezdKaaqfg/viewform?usp=pp_url&entry.400550049=Mobile+Ads+SDK&entry.460850823=5004Q00002efy0jQAA&entry.80707362=00134080

2. Fill out all fields, and attach your file(s).

3. Please reply back on this thread when you have uploaded your file(s). Please do not share this link.

 

Regards,

Google Logo
Princess Pamela
Mobile Ads SDK Team
 



ref:_00D1U1174p._5004Q2efy0j:ref
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Mobile Ads SDK Forum Advisor

unread,
Oct 4, 2022, 3:30:57 AM10/4/22
to rbrtfl...@gmail.com, google-adm...@googlegroups.com

Hi Roberto,

I hope you're doing well.

I just want to circle back on this. The team is requesting a copy of a Charles Log capturing the behavior that is happening in your end. Kindly provide it to us privately. You can provide the requested details via Reply privately to author option. Once provided, we will share it to the rest of the team as additional information.

Reply all
Reply to author
Forward
0 new messages