首先,需要在项目中包含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关闭");
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。