Unity 中的插页式广告

InterstitialAds in Unity

我在 Unity 中有这段用于 InterstitialAds 的代码,我想在关卡关闭和新关卡开始时每次都启动这个全屏广告,所以我使用 OnDestroy 函数,但是什么时候我必须调用 interstitial.destroy(); ? Between: 游戏流畅运行的代码对吗??感谢所有回答,对不起我的英语:)

    public class GoogleAdsScript : MonoBehaviour
    {
        bool isLoaded = false;
        private InterstitialAd interstitial;
        private BannerView bannerView;

        void Start()
        {
            RequestInterstitial();

            //RequestBanner();
        }

        void OnDestroy()
        {
            if (interstitial.IsLoaded() && isLoaded == false)
            {
                interstitial.Show();
                isLoaded = true;
            }
        }

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

            // Initialize an InterstitialAd.
             interstitial = new InterstitialAd(adUnitId);
            // Create an empty ad request.
            AdRequest request = new AdRequest.Builder().Build();
            // Load the interstitial with the request.
            interstitial.LoadAd(request);
        }

        private void RequestBanner()
        {
    #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/6300978111";
    #elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    #else
            string adUnitId = "unexpected_platform";
    #endif

            // Create a 320x50 banner at the top of the screen.
            bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
            // Create an empty ad request.
            AdRequest request = new AdRequest.Builder().Build();
            // Load the banner with the request.
            bannerView.LoadAd(request);
        }


    }

如果持有InterstitialAd实例引用(interstitial)的脚本(GoogleAdsScript)即将销毁,您应该调用interstitial.destroy();。您这样做是为了不会丢失参考。

我的建议是将 GoogleAdsScript 脚本中的重要函数设为 public。将 GoogleAdsScript 附加到名为 AdsObj 的游戏对象。 将DontDestroyOnLoad(transform.gameObject);放在GoogleAdsScript脚本的Awake()函数中,使其在加载新场景时不会被破坏。您现在可以从其他脚本访问 GoogleAdsScript 以显示或隐藏广告。

public class OtherScript : MonoBehaviour
{
    public GoogleAdsScript googleAds;

    void Start()
    {
        googleAds = GameObject.Find("AdsObj").GetComponent<GoogleAdsScript>();
        googleAds.RequestInterstitial();//Assumes that RequestInterstitial is now public
    }
}

没有理由再销毁 GoogleAdsScript 脚本了。

这对我有用...

伪:

  1. 创建静态广告管理器脚本(参见下面的示例)
  2. 请求关卡开头的插页式广告。
  3. 关卡完成后,检查广告是否已加载然后显示。
  4. 在那之后,加载你的下一级或菜单场景(不要破坏插页式广告!)

静态 AdManagerScript 示例:

using GoogleMobileAds.Api;

public static class AdManagerScript
{
    private static InterstitialAd interstitial;

    // Call this method only once when your app starts
    public static void StartMobileAdsSDK()
    {
        #if UNITY_ANDROID
            string appId = "...your admob appid here...";
            MobileAds.Initialize(appId);
        #endif
    }

    public static void RequestInterstitial()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/1033173712";
            interstitial = new InterstitialAd(adUnitId);
            AdRequest request = new AdRequest.Builder().Build();
            interstitial.LoadAd(request);
        #endif
    }

    public static void ShowInterstitital()
    {
        #if UNITY_ANDROID
            if (interstitial.IsLoaded())
            {
                interstitial.Show();
            }
        #endif
    }

接下来,在您的关卡脚本中:

...

void Start() {
    AdManager.RequestInterstitial();
}

...

void GameLevelFinished() {
    AdManager.ShowInterstitial();
    SceneManager.LoadScene("NextLevelName");
}

此外,记得在您的应用启动时调用一次 StartMobileAdsSDK。

Admanager.StartMobileAdsSDK();

使用预编译器指令 (#if #endif) 避免尝试在 Unity 编辑器中加载广告。控制台会有一些提示,但这是正常的。