Admob没有显示广告(统一)。编译没有错误。游戏顺利在编辑器中运行。在手机

时间:2018-05-07 02:01:09

标签: unity3d admob

编辑:经过12个小时,没有休息,完全沮丧,我想我应该创建一个新项目,然后把所有东西都移到那里。它奏效了!迁移只用了大约10分钟!

我是Unity的初学者,我已完全完成了我的游戏,但实施广告在过去的48小时里一直困扰着我。

无论我做什么,我都无法绕过它。我到处搜索,删除我的插件并重新导入它们。考虑到每一种情况都会阻止广告显示/游戏崩溃并相应地调整我的游戏,但没有成功。

首先,我的应用程序会在我的手机上正常运行,但最近(在一些代码更改后)它只是在启动时崩溃。以下是导致我的应用程序崩溃的唯一2个脚本,也是引用广告的2个脚本。

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

public class adsScript : MonoBehaviour 
{
    private RewardBasedVideoAd rewardBasedVideo;

    public void Start()
    {
        DontDestroyOnLoad(gameObject);
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

        // Called when an ad request has successfully loaded.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        // Called when an ad request failed to load.
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
        // Called when an ad is shown.
        rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
        // Called when the ad starts to play.
        rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
        // Called when the user should be rewarded for watching a video.
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        // Called when the ad is closed.
        rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
        // Called when the ad click caused the user to leave the application.
        rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;

        string appId = "ca-app-pub-2080245637481049~6344779320";
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        // Get singleton reward based video ad reference.

        this.RequestRewardBasedVideo();
    }

    private void RequestRewardBasedVideo()
    {
        string adUnitId = "ca-app-pub-2080245637481049/3426756348";

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded video ad with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);
    }

    #region RewardBasedVideo callback handlers

    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("video loaded");
    }

    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print(
            "HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
    }

    public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
    }

    public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        PlayerPrefs.SetInt("AdLife", 2);
        MonoBehaviour.print(
            "HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
    }

    public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
    }

    #endregion

}

和我访问广告的脚本(来自其他场景)

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

public class openAdVideo: MonoBehaviour 
{

    public void PlayAdVideo()
    {
        if (RewardBasedVideoAd.Instance.IsLoaded())
        {
            try
            {
                RewardBasedVideoAd.Instance.Show();
            }
            catch(Exception e)
            {
                Debug.Log(e.Message);
            }
        }
        else
        {
        }
    }
}

1 个答案:

答案 0 :(得分:0)

迁移到新项目解决了该问题。