【问题标题】:InterstitialAds in UnityUnity 中的插页式广告
【发布时间】:2016-08-11 17:28:51
【问题描述】:

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


    }

【问题讨论】:

    标签: c# unity3d ads interstitial unityads


    【解决方案1】:

    如果持有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 脚本了。

    【讨论】:

    • 必须调用 interstitial.destroy();,如果我在每三级使用广告?
    • 你听起来好像不明白我的回答。如果其中任何部分令人困惑,您可以提出问题。如果你做interstitial = new InterstitialAd(adUnitId);,你也必须在OnDestroyOnDisable函数中做interstitial.destroy();。我向您展示了一种在您的场景中只有一个 GoogleAdsScript 包含所有内容的方法。如果您按照我的回答中的方向进行操作,则不必销毁它
    • 我更改脚本:void Update() { if (interstitial.IsLoaded() &amp;&amp; isLoaded == false) { interstitial.Show(); isLoaded = true; } } void OnDestroy() { if (interstitial.IsLoaded()) interstitial.Destroy(); } 这是干净的方式吗?
    • 这就是我在回答的第一部分中的意思,但我建议您改为执行第二部分,因为您所做的不同。由于您想在关卡和新关卡开始时展示广告,因此在关卡为 Destoying 时拨打interstitial.Destroy() 不是一个好主意。仅当用户单击将加载下一个级别的按钮时,才应执行此操作。如果关卡自动加载,用户可能没有机会看到广告。选择第二种解决方案
    • 有下一级按钮,不会自动加载
    【解决方案2】:

    这对我有用...

    伪:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多