【问题标题】:Implementing an Interstitial Ad using AdMob in Unity在 Unity 中使用 AdMob 实现插页式广告
【发布时间】:2020-09-11 06:50:08
【问题描述】:

我正在使用 Unity 和 C# 为 Android 制作 2D 游戏。 我的横幅广告出现故障。我在这张图片中更好地解释了它。 我遇到了插页式广告根本没有出现的问题。 我希望它在 3 次死亡后出现。 你可以在下面看到我的代码:

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

public class Ad3 : MonoBehaviour
{
    private BannerView bannerView;                              // Banner Ad
    public InterstitialAd interstitial;                         // Interstitial Ad
    static int loadCount = 0;                                   // Interstitial Ad

    public void Awake()
    {
        MobileAds.Initialize(initStatus => { });                // Initializing Ad SDK
    }

    public void Start()
    {
        this.RequestBanner();                                   // Show Banner AD in the Start
        if (loadCount % 3 == 0)                                 // Interstitial Ad - only show ad every third death - Got this line from another stackoverflow question - so, is loadCount universal?
        {
            this.ShowInterstitial();
        }
        loadCount++;
    }
    
    public void RequestBanner()
    {
        string adUnitId = "ca-app-pub-3940256099942544/6300978111";
        this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);

        AdRequest request = new AdRequest.Builder().Build();    // Create an empty ad request.
        this.bannerView.LoadAd(request);                        // Load the banner with the request.
    }
    
    public void RequestInterstitial()                                   // Interstitial Ad
    {
        string InterstitialAdID = "ca-app-pub-3940256099942544/1033173712";
        this.interstitial = new InterstitialAd(InterstitialAdID);       // Initialize an InterstitialAd.
        
        AdRequest request = new AdRequest.Builder().Build();            // Create an empty ad request.
        this.interstitial.LoadAd(request);                              // Load the interstitial with the request.
    }
    public void ShowInterstitial()                                   // Interstitial Ad
    {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }  
    }

}

感谢您的所有反馈。

谢谢:)

【问题讨论】:

    标签: c# unity3d admob ads interstitial


    【解决方案1】:

    问题是你永远不会在任何地方调用“RequestInterstitial()” 需要请求插页式才能显示 注意:检查在另一个脚本中显示广告的时间,该脚本是否会调用该函数来显示插页式广告:

    SingletonTest.Instance.TheFunctionThatShowInsterstitial();

    单例示例:

     public class SingletonTest : MonoBehaviour
     {
       public static SingletonTest Instance;
    
       private void Awake()
       {
         if (Instance == null)
         {
             Instance = this;
             
         }
         else if(Instance != this)
         {
             Destroy(gameObject);
             return;
         }
         DontDestroyOnLoad(gameObject);
       }
    }
    

    【讨论】:

    • 哦,对了。那是愚蠢的大声笑。现在会试试。谢谢你的回复:)
    • 我一开始就叫它。仍然没有出现:/ this.RequestInterstitial();
    • 如果您请求插页式广告并希望同时显示将不会显示任何内容,因为很有可能尚未加载。每次您进入该场景时,都会重新加载开始,因此插页式广告将再次为空,必须加载请求。我的建议是使用单例模式并使用 DontDestroyOnLoad 对象。
    • 我明白你在说什么。问题是由于我缺乏编码知识,我不知道如何实现它。我创建了一个广告管理器(在层次结构中)并将广告脚本附加到它。我在清醒时制作了 RequestInterstitial 和 DontDestroyOnLoad。在开始时,如果 loadCount 为 3,我添加了 ShowInterstitial。我观看了许多 YouTube 视频并阅读了许多指南,但仍然没有得到它。问题是 - 如你所知 - 每个人都有自己的编码方式,这与我的不同。这不是 1+1=2。更新脚本:pastebin.com/2TLha7KW
    • 非常感谢您的支持。在比较您的第一个回复中的代码时,我之前在 GameControl.cs Awake() 脚本中设置了确切的“实例”行,除了 DontDestroyOnLoad(gameObject);..... 我猜,C# 只允许一个实例?如果是这样,那么这可能行不通:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多