【问题标题】:Issue with Admob Interstitial Unity 3DAdmob 插页式 Unity 3D 问题
【发布时间】:2019-01-11 15:58:30
【问题描述】:

我已关注 Google Admob step by step tutorial 在我的 3D Unity 游戏上实施插页式广告,但在我的设备上运行游戏时未显示插页式广告。

你知道我的代码出了什么问题,以及如何解决吗?

广告脚本

    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);
    }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    对我来说,问题似乎发生了,因为插页式广告需要一段时间才能加载(有时长达 3 秒)。你试图调用它太快,它没有加载,所以什么也没有发生。我所做的是不断尝试在 void Update 中显示插页式广告,直到它显示(使用名为 shown 的布尔值):

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

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

    【讨论】:

    • 我在玩家输球时和重新开始场景之前调用了 ShowInterstitial 函数,它起作用了!谢谢!
    【解决方案2】:

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

    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();
            }
         }
    
    }
    

    那么当你想显示插页式广告时

    AdController.instance.showInterstitial();
    

    它可能有语法错误或其他东西,但你已经明白了。

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多