簡體   English   中英

Admob 橫幅在 Unity 中不可見但仍可點擊

[英]Admob banner is invisible but still clickable in Unity

我正在開發一個根據場景顯示橫幅的應用程序。 我使用 Show 和 Hide 來控制這種行為。

第一次顯示橫幅時效果很好,但第二次(隱藏后)橫幅沒有出現。 但是,橫幅區域仍然可以點擊並且按預期執行。

我正在使用 AdMob 插件的最新版本來實現統一 (v3.15.1),但我無法找到任何解決問題的方法。

這是我的代碼:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;

public class AdsManager //Ads information: https://developers.google.com/admob/unity (see left menu fo detailed info of each ad type)
{
    public static AdsManager instance;
    public BannerView bannerAd;
    public InterstitialAd  interstitialAd;
    public RewardBasedVideoAd rewardedVideoAd;

    #if UNITY_EDITOR
    private static String appId = "unused";
    private static String bannerAdId = "unused";
    private static String interistitialAdId = "unused";
    private static String rewardedVideAdId = "TBD";
    #elif UNITY_ANDROID
    private static String appId = "ca-app-pub-6685658831663319~7807395346";
    private static String bannerAdId = "ca-app-pub-3940256099942544/6300978111"; //"ca-app-pub-6685658831663319/9607562172";
    private static String interistitialAdId = "ca-app-pub-3940256099942544/1033173712"; //"ca-app-pub-6685658831663319/4875778545";
    private static String rewardedVideAdId = "ca-app-pub-3940256099942544/5224354917"; //"ca-app-pub-6685658831663319/2971919290";
    #elif UNITY_IOS
    private static String appId = "ca-app-pub-6685658831663319~7807395346";
    private static String bannerAdId = "ca-app-pub-6685658831663319/9607562172";
    private static String interistitialAdId = "ca-app-pub-6685658831663319/4875778545";
    private static String rewardedVideAdId = "ca-app-pub-6685658831663319/2971919290";
    #else
    private static String appId = "unexpected_platform";
    private static String bannerAdId = "unexpected_platform";
    private static String interistitialAdId = "unexpected_platform";
    private static String rewardedVideAdId = "unexpected_platform";
    #endif


    public enum TypeOfAd
    {
        Banner,
        Interestitial, 
        RewardedVideo
    }

    public AdsManager()
    {
        Debug.Log("Initializing a new AdsManager.");
        if (instance == null)
        {
            instance = this;
            Setup();
            Debug.Log("AdsManager initialization successful.");
        }
        else
        {
            Debug.Log("AdsManager already exists. New initialization unsuccessful.");
        }
    }

    private void Setup()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.bannerAd = new BannerView(bannerAdId, AdSize.SmartBanner, AdPosition.Bottom); //Info to chang banner size: https://developers.google.com/admob/unity/banner#banner_sizes
        SetupBannerEventHandlers();

        this.interstitialAd = new InterstitialAd(interistitialAdId);
        SetupInterstitialEventHandlers();

        this.rewardedVideoAd = RewardBasedVideoAd.Instance;
        SetupRewardedVideoAdsEventHandlers();

        //Load firsts ads
        instance.LoadAd(TypeOfAd.Interestitial);
        instance.LoadAd(TypeOfAd.Banner);
    }

    private void SetupBannerEventHandlers()
    {
        this.bannerAd.OnAdLoaded += HandlerOnBannerAdLoaded;
        this.bannerAd.OnAdFailedToLoad += HandlrOnBannerAdFailedToLoad;
        this.bannerAd.OnAdOpening += HandlerOnBannerAdOpening;
        this.bannerAd.OnAdClosed += HandlerOnBannerAdClosed;
        this.bannerAd.OnAdLeavingApplication += HandlerOnBannerAdLeavingApplication;
    }

    private void SetupInterstitialEventHandlers()
    {
        this.interstitialAd.OnAdLoaded += HandlerOnInterstitialAdLoaded;
        this.interstitialAd.OnAdFailedToLoad += HandlerOnInterstitialAdFailedToLoad;
        this.interstitialAd.OnAdOpening += HandlerOnInterstitialAdOpening;
        this.interstitialAd.OnAdClosed += HandlerOnInterstitialAdClosed;
        this.interstitialAd.OnAdLeavingApplication += HandlerOnInterstitialAdLeavingApplication;
    }

    private void SetupRewardedVideoAdsEventHandlers()
    {
        this.rewardedVideoAd.OnAdLoaded += HandlerRewardVideoAdLoaded;
        this.rewardedVideoAd.OnAdFailedToLoad += HandlerRewardVideoAdFailedToLoad;
        this.rewardedVideoAd.OnAdOpening += HandlerRewardVideoAdOpening;
        this.rewardedVideoAd.OnAdStarted += HandlerRewardVideoAdStarted;
        this.rewardedVideoAd.OnAdRewarded += HandlerRewardVideoAdRewarded;
        this.rewardedVideoAd.OnAdClosed += HandlerRewardVideoAdClosed;
        this.rewardedVideoAd.OnAdLeavingApplication += HandlerRewardVideoAdLeftApplication;
    }

    public void LoadAd(TypeOfAd typeOfAd)
    {
        if (DataManager.instance.showAds)
            switch (typeOfAd)
            {
                case TypeOfAd.Banner:
                    Debug.Log("Loading Banner ad");
                    this.bannerAd.LoadAd(new AdRequest.Builder().Build());  //After loading it, it will be automatically displayed
                    break;

                case TypeOfAd.Interestitial:
                    Debug.Log("Loading Interestitial ad");
                    if (!this.interstitialAd.IsLoaded())
                        this.interstitialAd.LoadAd(new AdRequest.Builder().Build());

                    break;

                case TypeOfAd.RewardedVideo:
                    Debug.Log("Loading RewardedVideo ad");
                    if (!this.rewardedVideoAd.IsLoaded())
                        this.rewardedVideoAd.LoadAd(new AdRequest.Builder().Build(), rewardedVideAdId);
                    break;
            }
    }

    public bool ShowAd(TypeOfAd typeOfAd)
    {
        if (DataManager.instance.showAds)
            switch (typeOfAd)
            {
                case TypeOfAd.Banner:
                    Debug.Log("Showing Banner ad");
                    this.bannerAd.Show(); //No avaliable check to know if the ad has been loaded...
                    return true; //...so maybe the return is true but the ad is not being displayed because it was not loaded before.


                case TypeOfAd.Interestitial:
                    Debug.Log("Showing Interestitial ad");
                    if (this.interstitialAd.IsLoaded())
                    {
                        this.interstitialAd.Show();
                        return true;
                    }
                    else
                    {
                        Debug.LogWarning("Trying to show InterstitialAd but it is not loaded");
                        //TBD: Automaitcally load?
                    }
                    break;


                case TypeOfAd.RewardedVideo:
                    Debug.Log("Showing RewardedVideo ad");
                    if (this.rewardedVideoAd.IsLoaded())
                    {
                        this.rewardedVideoAd.Show();
                        return true;
                    } else {
                        Debug.LogWarning("Trying to show RewardedBasedVideoAd but it is not loaded");
                        //TBD: Automaitcally load?
                    }
                    break;
            }

        return false;
    }

    public void QuitAd(TypeOfAd typeOfAd)
    {
        switch (typeOfAd)
        {
            case TypeOfAd.Banner:
                Debug.Log("Quiting Banner ad");
                bannerAd.Hide();
                break;


            case TypeOfAd.Interestitial:
                Debug.Log("Quiting Interestitial ad");
                Debug.LogError("QuitAd Interestitial Not Implemented");
                break;


            case TypeOfAd.RewardedVideo:
                Debug.Log("Quiting RewardedVideo ad");
                Debug.LogError("QuitAd RewardedVideo Not Implemented");
                break;
        }
    }

    //BANNER EVENT HANDLERS
    public void HandlerOnBannerAdLoaded(object sender, EventArgs args) { } // Called when an ad request has successfully loaded.
    public void HandlrOnBannerAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Debug.LogWarning("Banner failed to load: " + args.Message); LoadAd(TypeOfAd.Banner); } // Called when an ad request failed to load.
    public void HandlerOnBannerAdOpening(object sender, EventArgs args) { } // Called when an ad is clicked.
    public void HandlerOnBannerAdClosed(object sender, EventArgs args) { } // Called when the user returned from the app after an ad click.
    public void HandlerOnBannerAdLeavingApplication(object sender, EventArgs args) { } // Called when the ad click caused the user to leave the application.

    //INTERSTITIAL EVENT HANDLERS
    public void HandlerOnInterstitialAdLoaded(object sender, EventArgs args) { } // Called when an ad request has successfully loaded.
    public void HandlerOnInterstitialAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Debug.LogWarning("Interstitial failed to load: " + args.Message); LoadAd(TypeOfAd.Interestitial); } // Called when an ad request failed to load.
    public void HandlerOnInterstitialAdOpening(object sender, EventArgs args) { } // Called when an ad is shown.
    public void HandlerOnInterstitialAdClosed(object sender, EventArgs args) { } // Called when the ad is closed.
    public void HandlerOnInterstitialAdLeavingApplication(object sender, EventArgs args) { } // Called when the ad click caused the user to leave the application.

    //REWARDED VIDEO AD EVENT HANDLERS
    public void HandlerRewardVideoAdLoaded(object sender, EventArgs args) { } // Called when an ad request has successfully loaded.
    public void HandlerRewardVideoAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) { Debug.LogWarning("RewardedVideo failed to load: " + args.Message); LoadAd(TypeOfAd.RewardedVideo); } // Called when an ad request failed to load.
    public void HandlerRewardVideoAdOpening(object sender, EventArgs args) { } // Called when an ad is shown.
    public void HandlerRewardVideoAdStarted(object sender, EventArgs args) { } // Called when the ad starts to play.
    public void HandlerRewardVideoAdRewarded(object sender, Reward args) { Debug.Log("Rewarded video ad completed. Reward info: " + args.Amount + " of " + args.Type); } // Called when the user should be rewarded for watching a video.
    public void HandlerRewardVideoAdClosed(object sender, EventArgs args) { } // Called when the ad is closed.
    public void HandlerRewardVideoAdLeftApplication(object sender, EventArgs args) { } // Called when the ad click caused the user to leave the application.

}

要顯示廣告,我只需調用 ShowAd 方法並隱藏它們,我調用 QuitAd 方法。 我認為兩者都按預期執行,因為它們在第一次被調用時工作,並且因為在 HideAd 被稱為“橫幅區域”的場景中是不可點擊的,但是當調用 ShowAd 方法時,您可以單擊不可見的橫幅。

感謝您的任何幫助!

我通過銷毀QuitAd方法中的橫幅廣告解決了該問題:

public void QuitAd(TypeOfAd typeOfAd)
{
    switch (typeOfAd)
    {
        case TypeOfAd.Banner:
            Debug.Log("Quiting Banner ad");
            bannerAd.Destroy();
            break;


        case TypeOfAd.Interestitial:
            Debug.Log("Quiting Interestitial ad");
            Debug.LogError("QuitAd Interestitial Not Implemented");
            break;


        case TypeOfAd.RewardedVideo:
            Debug.Log("Quiting RewardedVideo ad");
            Debug.LogError("QuitAd RewardedVideo Not Implemented");
            break;
    }
}

然后,我修改了ShowAd方法,在顯示橫幅之前先將其加載:

public bool ShowAd(TypeOfAd typeOfAd)
{
    if (DataManager.instance.showAds)
        switch (typeOfAd)
        {
            case TypeOfAd.Banner:
                Debug.Log("Showing Banner ad");
                LoadAd(TypeOfAd.Banner); //Every time the banner is asked to be shown it will try to load before being shown.
                this.bannerAd.Show(); //Will be show after loading
                return true;


            case TypeOfAd.Interestitial:
                Debug.Log("Showing Interestitial ad");
                if (this.interstitialAd.IsLoaded())
                {
                    this.interstitialAd.Show();
                    return true;
                }
                else
                {
                    Debug.LogWarning("Trying to show InterstitialAd but it is not loaded");
                    //TBD: Automaitcally load?
                }
                break;


            case TypeOfAd.RewardedVideo:
                Debug.Log("Showing RewardedVideo ad");
                if (this.rewardedVideoAd.IsLoaded())
                {
                    this.rewardedVideoAd.Show();
                    return true;
                } else {
                    Debug.LogWarning("Trying to show RewardedBasedVideoAd but it is not loaded");
                    //TBD: Automaitcally load?
                }
                break;
        }

    return false;
}

但是,我不知道這是否是一個合適的解決方案,因為每次必須顯示橫幅時(在未顯示的場景之后),都​​會進行新的加載請求。

另外,此“解決方案”只是針對同一目標的不同方法,而不是原始方法的解決方案。

因此,如果有人知道原始代碼為什么不起作用,那么我將非常感謝分享這些知識。

在我的情況下,由於某種原因畫布與橫幅重疊,我只是在PlayerSettings -> Resolution and Presentation 中取消選中“ Render Over native UI ”,現在它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM