0

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

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


    }
4

2 回答 2

0

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

我的建议是让GoogleAdsScript脚本中的重要功能成为public. 附加GoogleAdsScript到名为AdsObj的游戏对象。加入脚本DontDestroyOnLoad(transform.gameObject);Awake()功能,GoogleAdsScript这样在加载新场景时它不会被破坏。您现在可以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脚本了。

于 2016-08-11T19:14:33.200 回答
0

这对我有用...

伪:

  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 编辑器中加载广告。控制台中会有一些消息,但这是正常的。

于 2018-09-24T09:32:47.470 回答