You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity游戏每5局展示插屏广告失效问题求助

Unity插屏广告每5次失败后不触发的问题排查与修复

我写了一套Unity游戏脚本,希望玩家每失败5次后展示插屏广告,但测试时广告无法正常触发——只有把gamesToShowAd设为1时广告才会显示。试了多种实现方式都没用,附上GameManager和AdsManager的代码,求帮忙排查问题。

GameManager 脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public GameObject gameOverCanvas;
    public AdsManager ads;
    int gameCount = 0;
    int gamesToShowAd = 5;

    private void Start()
    {
        Time.timeScale = 1;
        ads.ShowBanner();
        gameCount = 0;
    }

    public void GameOver()
    {
        gameOverCanvas.SetActive(true);
        Time.timeScale = 0;

        gameCount++; //increment game count

        if (gameCount >= gamesToShowAd) // check if player played enough games to show ad
        {
            ads.PlayAd();
            gameCount = 0; // reset counter
        }
    }

    public void Replay()
    {
        SceneManager.LoadScene(0);
    }
}

AdsManager 脚本

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;

public class AdsManager : MonoBehaviour, IUnityAdsListener
{
#if UNITY_IOS
    string gameId = "#######";
#else
    string gameId = "#######";
#endif

    Action onRewardedAdSuccess;

    // Start is called before the first frame update
    void Start()
    {
        Advertisement.Initialize(gameId);
        Advertisement.AddListener(this);
        ShowBanner();
    }

    public void PlayAd()
    {
        if(Advertisement.IsReady("Interstitial_Android"))
            Advertisement.Show("Interstitial_Android");
    }

    public void PlayRewardedAd(Action onSuccess)
    {
        onRewardedAdSuccess = onSuccess;
        if(Advertisement.IsReady("Rewarded_Android"))
        {
        Advertisement.Show("Rewarded_Android");
        }
        else
        {
            Debug.Log("Rewarded ad is not ready!");
        }
    }

    public void ShowBanner()
    {
        if (Advertisement.IsReady("Banner_Android"))
        {
            Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
            Advertisement.Banner.Show("Banner_Android");
        }
        else
        {
            StartCoroutine(RepeatShowBanner());
        }
    }

    public void HideBanner()
    {
        Advertisement.Banner.Hide();
    }

    IEnumerator RepeatShowBanner()
    {
        yield return new WaitForSeconds(1);
        ShowBanner();
    }

    public void OnUnityAdsReady(string placementId)
    {
        Debug.Log("ADS ARE READY!");
    }

    public void OnUnityAdsDidError(string message)
    {
        Debug.Log("ERROR: " + message);
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        Debug.Log("VIDEO STARTED!");
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        if (placementId == "Rewarded_Android" && showResult == ShowResult.Finished)
        {
            onRewardedAdSuccess.Invoke();
        }
    }
}

问题排查与修复方案

1. 核心问题:场景重载导致计数器重置

每次调用Replay()加载场景时,原GameManager实例会被销毁,新实例的gameCount会在Start()中被重置为0,导致永远无法累积到5次失败。

修复方式二选一:

  • 方案A:单例模式保留GameManager
    修改GameManager的Awake和Start方法,让实例跨场景保留:

    public class GameManager : MonoBehaviour
    {
        public static GameManager Instance;
        public GameObject gameOverCanvas;
        public AdsManager ads;
        int gameCount = 0;
        int gamesToShowAd = 5;
    
        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }
    
        private void Start()
        {
            Time.timeScale = 1;
            ads.ShowBanner();
            // 移除gameCount = 0,避免场景重载时重置计数器
        }
    
        // 其他方法保持不变
    }
    
  • 方案B:用PlayerPrefs持久化计数器
    适合需要跨游戏会话保存失败次数的场景:

    private void Start()
    {
        Time.timeScale = 1;
        ads.ShowBanner();
        gameCount = PlayerPrefs.GetInt("GameFailCount", 0); // 从本地读取计数
    }
    
    public void GameOver()
    {
        gameOverCanvas.SetActive(true);
        Time.timeScale = 0;
    
        gameCount++;
        PlayerPrefs.SetInt("GameFailCount", gameCount);
        PlayerPrefs.Save();
    
        if (gameCount >= gamesToShowAd)
        {
            ads.PlayAd();
            gameCount = 0;
            PlayerPrefs.SetInt("GameFailCount", 0);
            PlayerPrefs.Save();
        }
    }
    

2. 广告位平台适配问题

当前PlayAd()只判断了Android的插屏广告位,若在iOS平台测试,Advertisement.IsReady()会返回false,导致广告不显示。

修复:

public void PlayAd()
{
    string interstitialId;
#if UNITY_IOS
    interstitialId = "Interstitial_iOS"; // 替换为你的iOS插屏广告位ID
#else
    interstitialId = "Interstitial_Android";
#endif
    if(Advertisement.IsReady(interstitialId))
        Advertisement.Show(interstitialId);
    else
    {
        Debug.Log($"插屏广告 {interstitialId} 未准备好");
    }
}

3. 增加调试日志确认广告状态

修改OnUnityAdsReady方法,打印具体广告位ID,确认插屏广告是否真的准备完成:

public void OnUnityAdsReady(string placementId)
{
    Debug.Log($"广告位 {placementId} 已准备好!");
}

内容的提问来源于stack exchange,提问作者Niks Rodžers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:39:22