Admob非页内广告Unity 3D的问题

时间:2019-01-10 21:29:54

标签: c# unity3d

在3D Unity游戏中实施插页式广告时,我一直遵循Google Admob step by step tutorial,但是在我的设备上运行游戏时,插页式广告没有显示。

您知道我的代码有什么问题以及如何解决吗?

广告脚本

    private InterstitialAd interstitial;

    static int loadCount = 0;

    bool GameHasEnded = false;
    float RestartDelay = 1.5f;


    private void Start()
    {

        #if UNITY_ANDROID
           string appId = "ca-app-pub-3940256099942544/1033173712";
        #else
           string appId = "unexpected_platform";
        #endif

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.RequestInterstitial();

        if ((loadCount % 3) == 0)  // only show ad every third time
        {
            if (this.interstitial.IsLoaded())
            {
                this.interstitial.Show();
            }
        }
    }

    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().path);
        loadCount = loadCount + 1;
    }

    private void RequestInterstitial()
    {
       #if UNITY_ANDROID
           string adUnitId = "ca-app-pub-3940256099942544/1033173712";
       #else
           string adUnitId = "unexpected_platform";
       #endif

        // Initialize an InterstitialAd.
        this.interstitial = new InterstitialAd(adUnitId);

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();

        // Load the interstitial with the request.
        this.interstitial.LoadAd(request);
    }

2 个答案:

答案 0 :(得分:0)

对我来说,问题似乎是由于插页式广告加载需要一段时间(有时最多3秒)而发生的。您试图调用它的速度太快,并且没有加载,所以什么也没发生。我所做的就是不断尝试在无效更新中展示插页式广告,直到它显示为止(使用名为show的布尔值):

void Update () {
     if (shown == false) {
         ShowInterstitial();
     }
}
public void ShowInterstitial()
{
     if (interstitial.IsLoaded())
     {
         interstitial.Show();
         shown = true;
     } else    {
         Debug.Log ("Interstitial is not ready yet.");
     }    
}

您还可以在关卡的开头加载广告,然后仅在回合结束时调用它。

答案 1 :(得分:0)

您正在尝试启动admob并在每次重新启动时请求新的插页式广告。您必须创建一个AdmobController实例或其他实例并使用它。

database.runInTransaction(() -> {
                        try {
                            database.getMemberDAO().insert(response.body().getMembers());
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                database.getPostDAO().insert(response.body().getPosts());
                            } catch (Exception e) {

                                e.printStackTrace();
                            } finally {
                                try {
                                    database.getCommentDAO().insert(response.body().getComments());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                } finally {
                                    try {
                                        database.getLikeDAO().insert(response.body().getLikes());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    } finally {
                                        try {
                                            database.getPostImageDAO().insert(response.body().getPostImages());
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        } finally {
                                            database.endTransaction();

                                        }
                                    }
                                }
                            }
                        }

                    });

然后在您想要展示插页式广告时

public class AdManager{

   public static AdManager instance;

   private void Awake(){
       if(instance == null) {
         instance = this;
         DontDestroyOnLoad(gameObject);
       } else {
         Destroy(gameObject);
         return;
       }
   }


   private void Start()
   {


       #if UNITY_ANDROID
          string appId = "ca-app-pub-3940256099942544/1033173712";
       #else
          string appId = "unexpected_platform";
       #endif

       // Initialize the Google Mobile Ads SDK.
       MobileAds.Initialize(appId);

       this.RequestInterstitial();

   }

    public void showInterstitial(){
      if ((loadCount % 3) == 0)  // only show ad every third time
      {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
     }

}

它可能存在语法错误或某些错误,但是您已经明白了。

相关问题