googleads / googleads-mobile-unity

Official Unity Plugin for the Google Mobile Ads SDK
https://developers.google.com/admob/unity
Apache License 2.0
1.34k stars 1.09k forks source link

multiple native ads doen't work #3182

Closed nmtoan91 closed 3 months ago

nmtoan91 commented 4 months ago

[REQUIRED] Step 1: Describe your environment

[REQUIRED] Step 2: Describe the problem

Steps to reproduce:

I have multiple scenes and I tried to use multiple native ads id. But in the AdMob dashboard, only an ads id has Impressions. The rest of native ads show OK on my game but it show No Impression on admob dashboard.

Please see my attached image as the detail image

Relevant Code:

// TODO(you): code here to reproduce the problem
NVentimiglia commented 4 months ago

@nmtoan91 Do you have some minimal sample code to show your implementation?

nmtoan91 commented 4 months ago

@NVentimiglia

This is my script, I use it for multiple gameobjects in multiple scenes.

using GoogleMobileAds.Api;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MyAdmob_NativeAdsUI : MonoBehaviour
{
    public bool isloaded = false;
    public Image imageImage;
    public Image imageIcon;
    public Image imageButton;
    public Text textButton;
    //public RawImage imageAdsChoice;
    public Text textHeadline;
    public Text textDesciption;
    public GameObject ads;
    public GameObject adsLoading;
    bool isLoadedFirstTime = false;
    void Start()
    {
        StartCoroutine(StartupProcess());
    }

    IEnumerator StartupProcess()
    {
        isloaded = false;
        ads.SetActive(false);
        {
            adsLoading.SetActive(true);
            StartCoroutine(RequestAdsThread());
        }
        yield return null;
    }
    public void HideColliders()
    {
        ads.SetActive(false);
    }
    public void ShowColliders()
    {
        ads.SetActive(true);
    }

    public void OnClick()
    {
        Debug.Log("OnClick()");
    }

    IEnumerator RequestAdsThread()
    {

        while (true)
        {
            RequestNativeAd();
            yield return new WaitForSeconds(30);
        }
    }

    public bool nativeAdLoaded = false; 
    public List<Sprite> naticeAds_images = new List<Sprite>();
    public string nativeAds_Headline;
    public string nativeAds_bodyText;
    public NativeAd nativeAd = null;
    AdLoader adLoader;
    public string adsId;
    private void RequestNativeAd()
    {
        adLoader = new AdLoader.Builder(adsId)
     .ForNativeAd()
     .Build();
        adLoader.OnNativeAdClicked += AdLoader_OnNativeAdClicked;
        adLoader.OnNativeAdLoaded += HandleNativeAdLoaded;
        adLoader.OnAdFailedToLoad += HandleNativeAdFailedToLoad;
        adLoader.LoadAd(new AdRequest());
    }

    private void AdLoader_OnNativeAdClicked(object sender, EventArgs e)
    {
        Debug.Log("WWWWWWWWWWWWAdLoader_OnNativeAdClicked: (" + name + ") "  + e.ToString());
    }

    bool shouldHide = false;
    private void HandleNativeAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        Debug.LogError($"Native ad failed to load: admobId={adsId} \n " + args.LoadAdError.ToString());
        if (!isLoadedFirstTime)
        {
            //gameObject.SetActive(false);
            shouldHide = true;
        }
    }
    private void Update()
    {
        if (shouldHide)
        {
            shouldHide = false;
            ads.SetActive(false);
            adsLoading.SetActive(false);
        }
    }
    //IEnumerator AutoRequestAfterSeconds(float time =3)
    //{

    //}
    private void HandleNativeAdLoaded(object sender, NativeAdEventArgs args)
    {
        if (this == null)
        {
            Debug.LogError("this is NULL");
            return;
        }
        try
        {
            Debug.Log("NATIVE_ADS loaded for " + adsId);
            StartCoroutine(HandleNativeAdLoaded_(sender, args));
        }
        catch (Exception e) 
        {
            Debug.LogError(e);
        }

    }
    private IEnumerator HandleNativeAdLoaded_(object sender, NativeAdEventArgs args)
    {

        isLoadedFirstTime = true;
        ads.SetActive(false);
        adsLoading.SetActive(true);
        adsLoading.transform.SetAsLastSibling();
        yield return new WaitForSeconds(1);

        if (args == null || args.nativeAd == null)
        {
            Debug.LogError("HandleNativeAdLoaded_ " + (args == null));
            shouldHide = true;
        }
        else
        {

            nativeAd = args.nativeAd;
            naticeAds_images.Clear();
            Texture2D tex = nativeAd.GetIconTexture();
            if (tex != null)
            {
                if (imageIcon != null)
                {
                    try
                    {
                        imageIcon.sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
                    }
                    catch (Exception e)
                    {
                        throw new System.Exception($"toanstt catched 1: imageIcon:{imageIcon == null}  tex:{tex == null}:" + e.Message);
                    }
                }
                else
                {
                    string s = "";

                    Transform t = transform;
                    while (t != null)
                    {
                        s += t.name + "->";
                        t = t.parent;
                    }
                }
            }
            textHeadline.text = nativeAds_Headline = nativeAd.GetHeadlineText();
            textDesciption.text = nativeAds_bodyText = nativeAd.GetBodyText();
            var l = nativeAd.GetImageTextures();
            foreach (var i in l)
            {
                var tmp = Sprite.Create(i,
                    new Rect(0.0f, 0.0f, i.width, i.height), new Vector2(0.5f, 0.5f));
                naticeAds_images.Add(tmp);
                try
                {
                    imageImage.sprite = tmp;
                }
                catch (Exception e)
                {
                    throw new System.Exception("toanstt catched 2: " + e.Message);
                }
            }

            textButton.text = nativeAd.GetCallToActionText();

            if (!nativeAd.RegisterIconImageGameObject(imageIcon.gameObject))
            {
                Debug.LogError("ERROR RegisterIconImageGameObject");
            }

            List<GameObject> listimages = new List<GameObject>();
            listimages.Add(imageImage.gameObject);

            if (nativeAd.RegisterImageGameObjects(listimages) == 0)
            {
                Debug.LogError("ERROR RegisterImageGameObjects");
            }

            if (!nativeAd.RegisterCallToActionGameObject(imageButton.gameObject))
            {
                Debug.LogError("ERROR RegisterImageGameObjects");
            }

            ads.SetActive(true);
            adsLoading.SetActive(false);
            isloaded = true;
        }
    }

}
NVentimiglia commented 4 months ago

@nmtoan91

From the looks of it you are loading the same native ad id (adsId) in a loop every 30 seconds (RequestAdsThread).

nmtoan91 commented 4 months ago

@NVentimiglia I did that to refresh the ads just like banner ads. However, how can it lead to zero impressions?

NVentimiglia commented 3 months ago

@nmtoan91

I am going to need a minimal sample which I can run to diagnose this. Please submit a sample complete with ad unit id, publisher id, and clear replication steps to https://developers.google.com/admob/support.