适用于Unity(3.15.1)的Admob 2018程序包未在android移动设备中显示广告(横幅,插页式广告,奖励视频等)

时间:2019-03-02 01:55:58

标签: c# unity3d admob

我已经完成了一个统一的游戏,并且已经在自己的游戏中实现了Admob。但是广告没有投放。仅当我打开游戏并等待而不玩时,有时横幅会出现在屏幕顶部。我找不到问题。我将Unity从2018.3.6切换到最新版本,并尝试将版本降低到2017.1.1,但没有任何更改。当我导出游戏并将其加载到手机中时,没有任何变化。我更新了sdk,jdk等。我将admob软件包更新为3.15.1,没有任何变化。有什么可能是错的吗? 我的系统有类似的程序;

  • Unity 2018.3.6f1

  • SDK已更新

  • JDK已更新

  • Admob软件包3.15.1

我使用了过去曾经使用过的google admobs示例代码,但是两天后我的大脑即将爆炸。

当我无法实施admob时,我尝试使用Unity Ads,但是这次仅播放了视频,并且再次无法播放横幅广告或奖励视频。

有没有人试图用admob发布游戏并遇到我的问题?你能帮我吗?

我也对除admob以外的更好的货币化方法提出了想法。您提供给我使用哪个平台?

一切都很完美,以前发生了什么变化?我无法弄清楚新的admob软件包和jdk。

我还这样编辑了admob CS代码:

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

public class AdmobManager : MonoBehaviour
{
    public static AdmobManager Instance;

    private BannerView bannerView;
    private InterstitialAd interstitial;
    private RewardBasedVideoAd rewardBasedVideo;   

    void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
    }

    void Start()
    {
        MobileAds.Initialize("ca-app-pub-***********************"); // admob game id
        RequestBanner();
        RequestInterstitial();
        RequestShowRewardBasedVideo();

        ShowBanner();
    }

    private void RequestBanner()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "ca-app-pub-*************************";
#elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
            string adUnitId = "unexpected_platform";
#endif
        if (this.bannerView != null)
        {
            this.bannerView.Destroy();
        }

        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);

        // Register for ad events.
        RegisterDelegateForBanner();

        // Load a banner ad.
        bannerView.LoadAd(createAdRequest());
    }

    private void RequestInterstitial()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "ca-app-pub-*************************";
#elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
            string adUnitId = "unexpected_platform";
#endif
        // Clean up interstitial ad before creating a new one.
        if (this.interstitial != null)
        {
            this.interstitial.Destroy();
        }

        // Create an interstitial.
        interstitial = new InterstitialAd(adUnitId);

        // Register for ad events.
        RegisterDelegateForInterstitial();

        // Load an interstitial ad.
        interstitial.LoadAd(createAdRequest());
    }

    void RequestShowRewardBasedVideo()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "ca-app-pub-*************************";
#elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
            string adUnitId = "unexpected_platform";
#endif
        rewardBasedVideo = RewardBasedVideoAd.Instance;        

        RegisterDelegateForShowRewardedVideo();

        rewardBasedVideo.LoadAd(createAdRequest(), adUnitId);        
    }

    // Returns an ad request with custom ad targeting.
    private AdRequest createAdRequest()
    {
        return new AdRequest.Builder()
                //.AddTestDevice(AdRequest.TestDeviceSimulator)
                //.AddTestDevice("")
                //.AddKeyword("")
                //.SetGender(Gender.Male)
                //.SetBirthday(new DateTime(1985, 1, 1))
                //.TagForChildDirectedTreatment(false)
                //.AddExtra("color_bg", "9B30FF")
                .Build();
    }

    IEnumerator ShowBannerWhenReady()
    {
        while (bannerView == null)
        {
            yield return null;
        }
        bannerView.Show();
    }

    public void ShowBanner()
    {
        if (bannerView == null)
        {
            RequestBanner();
            StartCoroutine(ShowBannerWhenReady());
        }

        if (bannerView != null)
        {
            StartCoroutine(ShowBannerWhenReady());
        }
    }

    IEnumerator ShowInterstitialWhenReady()
    {
        while (!interstitial.IsLoaded())
        {
            yield return null;
        }

        interstitial.Show();
    }

    public void ShowInterstitial()
    {
        if (!interstitial.IsLoaded())
        {
            RequestInterstitial();
            StartCoroutine(ShowInterstitialWhenReady());
        }

        if (interstitial.IsLoaded())
        {
            StartCoroutine(ShowInterstitialWhenReady());
        }
    }

    IEnumerator ShowRewardedVideoWhenReady()
    {
        while (!rewardBasedVideo.IsLoaded())
        {
            yield return null;
        }
        rewardBasedVideo.Show();
    }

    public void ShowRewardBasedVideo()
    {
        if (!rewardBasedVideo.IsLoaded())
        {
            RequestShowRewardBasedVideo();
            StartCoroutine(ShowRewardedVideoWhenReady());
        }

        if (rewardBasedVideo.IsLoaded())
        {
            StartCoroutine(ShowRewardedVideoWhenReady());
        }
    }

    //-----------------------------------------------------------------------------

    void RegisterDelegateForBanner()
    {
        bannerView.OnAdLoaded += HandleAdLoaded;
        bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
        bannerView.OnAdLoaded += HandleAdOpened;
        bannerView.OnAdClosed += HandleAdClosed;
        bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
    }

    void UnRegisterDelegateForBanner()
    {
        bannerView.OnAdLoaded -= HandleAdLoaded;
        bannerView.OnAdFailedToLoad -= HandleAdFailedToLoad;
        bannerView.OnAdLoaded -= HandleAdOpened;
        bannerView.OnAdClosed -= HandleAdClosed;
        bannerView.OnAdLeavingApplication -= HandleAdLeftApplication;
    }

    void RegisterDelegateForInterstitial()
    {
        interstitial.OnAdLoaded += HandleInterstitialLoaded;
        interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
        interstitial.OnAdOpening += HandleInterstitialOpened;
        interstitial.OnAdClosed += HandleInterstitialClosed;
        interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
    }
    void UnRegisterDelegateForInterstitial()
    {
        interstitial.OnAdLoaded -= HandleInterstitialLoaded;
        interstitial.OnAdFailedToLoad -= HandleInterstitialFailedToLoad;
        interstitial.OnAdOpening -= HandleInterstitialOpened;
        interstitial.OnAdClosed -= HandleInterstitialClosed;
        interstitial.OnAdLeavingApplication -= HandleInterstitialLeftApplication;
    }

    void RegisterDelegateForShowRewardedVideo()
    {
        // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
        this.rewardBasedVideo.OnAdLoaded += this.HandleRewardBasedVideoLoaded;
        this.rewardBasedVideo.OnAdFailedToLoad += this.HandleRewardBasedVideoFailedToLoad;
        this.rewardBasedVideo.OnAdOpening += this.HandleRewardBasedVideoOpened;
        this.rewardBasedVideo.OnAdStarted += this.HandleRewardBasedVideoStarted;
        this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
        this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
        this.rewardBasedVideo.OnAdLeavingApplication += this.HandleRewardBasedVideoLeftApplication;
    }

    void UnRegisterDelegateForShowRewardedVideo()
    {
        // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
        this.rewardBasedVideo.OnAdLoaded -= this.HandleRewardBasedVideoLoaded;
        this.rewardBasedVideo.OnAdFailedToLoad -= this.HandleRewardBasedVideoFailedToLoad;
        this.rewardBasedVideo.OnAdOpening -= this.HandleRewardBasedVideoOpened;
        this.rewardBasedVideo.OnAdStarted -= this.HandleRewardBasedVideoStarted;
        this.rewardBasedVideo.OnAdRewarded -= this.HandleRewardBasedVideoRewarded;
        this.rewardBasedVideo.OnAdClosed -= this.HandleRewardBasedVideoClosed;
        this.rewardBasedVideo.OnAdLeavingApplication -= this.HandleRewardBasedVideoLeftApplication;
    }

    //--------------------------------------------------------------------------
    public void HandleAdLoaded(object sender, EventArgs args)
    {
        ShowBanner();
    }

    public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        UnRegisterDelegateForBanner();
        RequestBanner();
    }

    public void HandleAdOpened(object sender, EventArgs args)
    {

    }

    void HandleAdClosing(object sender, EventArgs args)
    {

    }

    public void HandleAdClosed(object sender, EventArgs args)
    {
        UnRegisterDelegateForBanner();
        RequestBanner();
    }

    public void HandleAdLeftApplication(object sender, EventArgs args)
    {

    }

    //------------------------------------------------------------

    public void HandleInterstitialLoaded(object sender, EventArgs args)
    {

    }

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        UnRegisterDelegateForInterstitial();
        RequestInterstitial();
    }

    public void HandleInterstitialOpened(object sender, EventArgs args)
    {

    }

    void HandleInterstitialClosing(object sender, EventArgs args)
    {

    }

    public void HandleInterstitialClosed(object sender, EventArgs args)
    {
        UnRegisterDelegateForInterstitial();
        RequestInterstitial();
    }

    public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    {

    }

    // ------------------------------------------------------------------

    private void HandleRewardBasedVideoLeftApplication(object sender, EventArgs e)
    {

    }

    private void HandleRewardBasedVideoClosed(object sender, EventArgs e)
    {
        UnRegisterDelegateForShowRewardedVideo();
        RequestShowRewardBasedVideo();
    }

    private void HandleRewardBasedVideoRewarded(object sender, Reward e)
    {
        //string type = e.Type;
        //double amount = e.Amount;
        ////Reawrd User here
        //print("User rewarded with: " + amount.ToString() + " " + type);

        //Debug.Log("Manually Control with DEBUG!!!");
    }

    private void HandleRewardBasedVideoStarted(object sender, EventArgs e)
    {

    }

    private void HandleRewardBasedVideoOpened(object sender, EventArgs e)
    {

    }

    private void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs e)
    {
        UnRegisterDelegateForShowRewardedVideo();
        RequestShowRewardBasedVideo();
    }

    private void HandleRewardBasedVideoLoaded(object sender, EventArgs e)
    {

    }
} 

0 个答案:

没有答案