学习笔记Mopub在Unity环境下的集成Banner和Interstitial

32次阅读

共计 4162 个字符,预计需要花费 11 分钟才能阅读完成。

首先,需要在项目中包含 MoPubUnity.unitypackage,包的下载地址:
https://github.com/mopub/mopu…

接下来点击菜单 Assets->Import Package->Custom Package,选择包文件

至此包就被包含进去了,包里面有简单的使用用例,不过不适合直接接入项目中。我们还是要写一个新的 Controller 来处理广告相关的事务。


新的 Controller 在本文中被命名为 MopubAdsController,首先将广告 id 暴露到 Inspector 界面:
    #region Inspector Variables
            [SerializeField] private string iOSBannerID;
            [SerializeField] private string iOSInterstitialID;
            [SerializeField] private string iOSVideoID;
            [Space]
            [SerializeField] private string AndroidBannerID;
            [SerializeField] private string AndroidInterstitialID;
            [SerializeField] private string AndroidVideoID;
    #endregion

由于广告请求的函数的参数不是 string,而是 string 数组,所以需要另三个函数来承接上面的 id

    private string[] _bannerAdUnits;
    private string[] _interstitialAdUnits;
    private string[] _rewardedVideoAdUnits;

接下来进入 Start 函数:

    void Start()
            {
    #if UNITY_IOS
                _bannerAdUnits = new string[] {iOSBannerID};
                _interstitialAdUnits = new string[] { iOSInterstitialID};
                _rewardedVideoAdUnits = new string[] { iOSVideoID};
    
    #elif UNITY_ANDROID || UNITY_EDITOR
    
                _bannerAdUnits = new string[] {AndroidBannerID};
                _interstitialAdUnits = new string[] { AndroidInterstitialID};
                _rewardedVideoAdUnits = new string[] { AndroidVideoID};
    
    #endif
    
                var anyAdUnitId = _bannerAdUnits[0];
                MoPub.InitializeSdk(new MoPub.SdkConfiguration
                {
                    AdUnitId = anyAdUnitId,
                    LogLevel = MoPubBase.LogLevel.MPLogLevelDebug,
                    MediatedNetworks = new MoPub.MediatedNetwork[]
                    {},});
    
                MoPub.LoadBannerPluginsForAdUnits(_bannerAdUnits);
    #if UNITY_IOS
                MoPub.CreateBanner(_bannerAdUnits[0], MoPubBase.AdPosition.BottomCenter);
    #elif UNITY_ANDROID
                MoPub.CreateBanner("REPLACE_BY_ANDROID_AD_UNIT_ID_HERE", MoPubAdPosition.BottomCenter);
    #endif
    
                MoPub.LoadInterstitialPluginsForAdUnits(_interstitialAdUnits);
                MoPub.RequestInterstitialAd(_interstitialAdUnits[0]);
    
                MoPub.LoadRewardedVideoPluginsForAdUnits(_rewardedVideoAdUnits);
    
    
                // 初始化各种广告, 现在还没有使用好几个 id 的需求,所以每个广告类型先都用一个 id,所以都取 [0]。按着官方用例,如果有多种 id 的话可以写一个 for 循环直接遍历创造就好
    
    
    
                // 以下是各种回调的承接,相当于 OC 的 XX.delegate = self;MoPubManager.OnSdkInitializedEvent += OnSdkInitializedEvent;
    
                MoPubManager.OnAdLoadedEvent += OnAdLoadedEvent;
                MoPubManager.OnAdFailedEvent += OnAdFailedEvent;
    
                MoPubManager.OnInterstitialLoadedEvent += OnInterstitialLoadedEvent;
                MoPubManager.OnInterstitialFailedEvent += OnInterstitialFailedEvent;
                MoPubManager.OnInterstitialDismissedEvent += OnInterstitialDismissedEvent;
    
                MoPubManager.OnRewardedVideoLoadedEvent += OnRewardedVideoLoadedEvent;
                MoPubManager.OnRewardedVideoFailedEvent += OnRewardedVideoFailedEvent;
                MoPubManager.OnRewardedVideoFailedToPlayEvent += OnRewardedVideoFailedToPlayEvent;
                MoPubManager.OnRewardedVideoClosedEvent += OnRewardedVideoClosedEvent;
            }

其中:


MoPubManager.OnSdkInitializedEvent += OnSdkInitializedEvent;

是 C# 的回调承接方法。其中 OnSdkInitializedEvent 是一个回调方法

 private void OnSdkInitializedEvent(string adUnitId)
{//_demoGUI.SdkInitialized();
}

而 MoPubManager.OnSdkInitializedEvent 则可以看做是一个装方法类型的数组,每当时间发生时,此数组中的方法会被依次调用,这也就是 C# 的回调原理。

接下来是 Controller 的 public 函数,供给外部调用,外部想显示广告只要调用响应函数就可以了

        public void ShowBannerAd()
        {MoPub.ShowBanner(_bannerAdUnits[0], true);

        }

        public void HideBannerAd()
        {MoPub.ShowBanner(_bannerAdUnits[0], false);
        }


        public void OnAdLoadedEvent(string adUnitId, string height)
        { }

        public bool ShowInterstitial()
        {
            bool show = false;
            Debug.Log("mopub 调试——interstitial 尝试播放");
            if (MoPub.IsInterstitialReady(_interstitialAdUnits[0]))
            {MoPub.ShowInterstitialAd(_interstitialAdUnits[0]);
                show = true;
                Debug.Log("mopub 调试——interstitial 播放");
            }
            else
            {MoPub.RequestInterstitialAd(_interstitialAdUnits[0]);
                Debug.Log("mopub 调试——interstitial 未加载成功,重新加载");
            }
            return show;
        }

接下来是各种回调函数


  //----------------------- 回调 -------------------------------------------
        //SDK 初始化成功
        private void OnSdkInitializedEvent(string adUnitId)
        { }


        // Banner Events
        //banner 读取成功
        private void OnAdLoadedEvent(string adUnitId, float height)
        {Debug.Log("mopub 调试——banner 读取成功");
        }

        //banner 读取失败
        private void OnAdFailedEvent(string adUnitId, string error)
        {Debug.Log("mopub 调试——banner 读取失败, id:" + adUnitId);
        }


        // Interstitial Events
        //interstitial 读取成功
        private void OnInterstitialLoadedEvent(string adUnitId)
        {Debug.Log("mopub 调试——interstitial 读取成功");
        }

        //interstitial 读取失败
        private void OnInterstitialFailedEvent(string adUnitId, string error)
        {MoPub.RequestInterstitialAd(_interstitialAdUnits[0]);
            Debug.Log("mopub 调试——interstitial 读取失败, id:" + adUnitId);
        }

        //interstitial 关闭
        private void OnInterstitialDismissedEvent(string adUnitId)
        {MoPub.RequestInterstitialAd(_interstitialAdUnits[0]);
            Debug.Log("mopub 调试——interstitial 关闭");
        }

正文完
 0